23 function utf8_str_pad($input, $length, $padStr =
' ', $type = STR_PAD_RIGHT) {
26 if ($length <= $inputLen) {
31 $padLen = $length - $inputLen;
33 if ($type == STR_PAD_RIGHT) {
34 $repeatTimes = ceil($padLen / $padStrLen);
35 return utf8_substr($input . str_repeat($padStr, $repeatTimes), 0, $length);
38 if ($type == STR_PAD_LEFT) {
39 $repeatTimes = ceil($padLen / $padStrLen);
40 return utf8_substr(str_repeat($padStr, $repeatTimes), 0, floor($padLen)) . $input;
43 if ($type == STR_PAD_BOTH) {
46 $padAmountLeft = floor($padLen);
47 $padAmountRight = ceil($padLen);
48 $repeatTimesLeft = ceil($padAmountLeft / $padStrLen);
49 $repeatTimesRight = ceil($padAmountRight / $padStrLen);
51 $paddingLeft =
utf8_substr(str_repeat($padStr, $repeatTimesLeft), 0, $padAmountLeft);
52 $paddingRight =
utf8_substr(str_repeat($padStr, $repeatTimesRight), 0, $padAmountLeft);
53 return $paddingLeft . $input . $paddingRight;
56 trigger_error(
'utf8_str_pad: Unknown padding type (' . $type .
')',E_USER_ERROR);