The problem is that the str_pad
function assumes that each character occupies one byte. When you use characters that are longer than one byte in length (such as ã ), the function starts to go wrong.
In StackOverflow in English there is a question about it and there are 4 answers to this problem. Judging by the comments, two of the answers have problems (including the accepted answer) and the other two should be appropriate (I have not tested them yet). All the answers given there consist of creating a different function capable of handling multibyte characters.
Here's Wes's solution :
function mb_str_pad($str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT, $encoding = NULL)
{
$encoding = $encoding === NULL ? mb_internal_encoding() : $encoding;
$padBefore = $dir === STR_PAD_BOTH || $dir === STR_PAD_LEFT;
$padAfter = $dir === STR_PAD_BOTH || $dir === STR_PAD_RIGHT;
$pad_len -= mb_strlen($str, $encoding);
$targetLen = $padBefore && $padAfter ? $pad_len / 2 : $pad_len;
$strToRepeatLen = mb_strlen($pad_str, $encoding);
$repeatTimes = ceil($targetLen / $strToRepeatLen);
$repeatedString = str_repeat($pad_str, max(0, $repeatTimes)); // safe if used with valid unicode sequences (any charset)
$before = $padBefore ? mb_substr($repeatedString, 0, floor($targetLen), $encoding) : '';
$after = $padAfter ? mb_substr($repeatedString, 0, ceil($targetLen), $encoding) : '';
return $before . $str . $after;
}
Here's the Ja͢ck solution :
function mb_str_pad($input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT, $encoding = 'UTF-8')
{
$input_length = mb_strlen($input, $encoding);
$pad_string_length = mb_strlen($pad_string, $encoding);
if ($pad_length <= 0 || ($pad_length - $input_length) <= 0) {
return $input;
}
$num_pad_chars = $pad_length - $input_length;
switch ($pad_type) {
case STR_PAD_RIGHT:
$left_pad = 0;
$right_pad = $num_pad_chars;
break;
case STR_PAD_LEFT:
$left_pad = $num_pad_chars;
$right_pad = 0;
break;
case STR_PAD_BOTH:
$left_pad = floor($num_pad_chars / 2);
$right_pad = $num_pad_chars - $left_pad;
break;
}
$result = '';
for ($i = 0; $i < $left_pad; ++$i) {
$result .= mb_substr($pad_string, $i % $pad_string_length, 1, $encoding);
}
$result .= $input;
for ($i = 0; $i < $right_pad; ++$i) {
$result .= mb_substr($pad_string, $i % $pad_string_length, 1, $encoding);
}
return $result;
}