PHP 8.5.0 Alpha 4 available for testing

Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

peter AT(no spam) dezzignz dot com
15 years ago
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

// multibyte string split

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;
}

// removes $rem at both ends

function mb_trim ($str, $rem = ' ') {
if (empty(
$str)) return false;
// convert to array
$arr = mbStringToArray($str);
$len = count($arr);
// left side
for ($i = 0; $i < $len; $i++) {
if (
$arr[$i] === $rem) $arr[$i] = '';
else break;
}
// right side
for ($i = $len-1; $i >= 0; $i--) {
if (
$arr[$i] === $rem) $arr[$i] = '';
else break;
}
// convert to string
return implode ('', $arr);
}

?>

<< Back to user notes page

To Top