本文整理汇总了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;
}
}