The function trim() has not failed me so far in my multibyte applications, but in case one needs a truly multibyte function, here it is. The nice thing is that the character to remove can be whitespace or any other specified character, even a multibyte character.
<?php
function mbStringToArray ($str) {
if (empty($str)) return false;
$len = mb_strlen($str);
$array = array();
for ($i = 0; $i < $len; $i++) {
$array[] = mb_substr($str, $i, 1);
}
return $array;
}
function mb_trim ($str, $rem = ' ') {
if (empty($str)) return false;
$arr = mbStringToArray($str);
$len = count($arr);
for ($i = 0; $i < $len; $i++) {
if ($arr[$i] === $rem) $arr[$i] = '';
else break;
}
for ($i = $len-1; $i >= 0; $i--) {
if ($arr[$i] === $rem) $arr[$i] = '';
else break;
}
return implode ('', $arr);
}
?>