41 $last4bytes = substr($string, -3);
42 $reversed = array_reverse(unpack(
"C*", $last4bytes));
43 if (($reversed[0] & 0x80) === 0x00)
45 elseif (($reversed[0] & 0xC0) === 0xC0)
46 return substr($string, 0, -1);
47 elseif (($reversed[1] & 0xE0) === 0xE0)
48 return substr($string, 0, -2);
49 elseif (($reversed[2] & 0xE0) === 0xF0)
50 return substr($string, 0, -3);
85 public static function pad($string, $padLen, $padStr =
' ', $padType = STR_PAD_RIGHT)
87 $strLength = mb_strlen($string);
88 $padStrLength = mb_strlen($padStr);
89 if (!$strLength && ($padType == STR_PAD_RIGHT || $padType == STR_PAD_LEFT))
93 if (!$padLen || !$padStrLength || $padLen <= $strLength)
99 $repeat = ceil(($padLen - $strLength) / $padStrLength);
100 if ($padType == STR_PAD_RIGHT)
102 $result = $string . str_repeat($padStr, $repeat);
103 $result = mb_substr($result, 0, $padLen);
105 else if ($padType == STR_PAD_LEFT)
107 $result = str_repeat($padStr, $repeat) . $string;
108 $result = mb_substr($result, -$padLen);
110 else if ($padType == STR_PAD_BOTH)
112 $length = ($padLen - $strLength) / 2;
113 $repeat = ceil($length / $padStrLength);
114 $result = mb_substr(str_repeat($padStr, $repeat), 0, floor($length))
116 .mb_substr(str_repeat($padStr, $repeat), 0, ceil($length));