本文整理匯總了PHP中UTF8::wpsubstr方法的典型用法代碼示例。如果您正苦於以下問題:PHP UTF8::wpsubstr方法的具體用法?PHP UTF8::wpsubstr怎麽用?PHP UTF8::wpsubstr使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類UTF8
的用法示例。
在下文中一共展示了UTF8::wpsubstr方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: str_pad
/**
* Pads a UTF-8 string to a certain length with another string. This is a
* UTF8-aware version of [str_pad](http://php.net/str_pad).
*
* $str = UTF8::wpstr_pad($str, $length);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string $str input string
* @param integer $final_str_length desired string length after padding
* @param string $pad_str string to use as padding
* @param string $pad_type padding type: STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH
* @return string
*/
public static function str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
if (UTF8::is_ascii($str) and UTF8::is_ascii($pad_str)) {
return str_pad($str, $final_str_length, $pad_str, $pad_type);
}
$str_length = UTF8::wpstrlen($str);
if ($final_str_length <= 0 or $final_str_length <= $str_length) {
return $str;
}
$pad_str_length = UTF8::wpstrlen($pad_str);
$pad_length = $final_str_length - $str_length;
if ($pad_type == STR_PAD_RIGHT) {
$repeat = ceil($pad_length / $pad_str_length);
return UTF8::wpsubstr($str . str_repeat($pad_str, $repeat), 0, $final_str_length);
}
if ($pad_type == STR_PAD_LEFT) {
$repeat = ceil($pad_length / $pad_str_length);
return UTF8::wpsubstr(str_repeat($pad_str, $repeat), 0, floor($pad_length)) . $str;
}
if ($pad_type == STR_PAD_BOTH) {
$pad_length /= 2;
$pad_length_left = floor($pad_length);
$pad_length_right = ceil($pad_length);
$repeat_left = ceil($pad_length_left / $pad_str_length);
$repeat_right = ceil($pad_length_right / $pad_str_length);
$pad_left = UTF8::wpsubstr(str_repeat($pad_str, $repeat_left), 0, $pad_length_left);
$pad_right = UTF8::wpsubstr(str_repeat($pad_str, $repeat_right), 0, $pad_length_right);
return $pad_left . $str . $pad_right;
}
}