note 94356 added to ref.mbstring
| From: | (nospam) | Date: | Fri, 30 Oct 2009 00:26:07 +0000 |
| Subject: | note 94356 added to ref.mbstring | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162270@lists.php.net to get a copy of this message | ||
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);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 124.217.180.162
----
Manual Page -- http://www.php.net/manual/en/ref.mbstring.php
Edit -- https://master.php.net/note/edit/94356
Del: integrated -- https://master.php.net/note/delete/94356/integrated
Del: useless -- https://master.php.net/note/delete/94356/useless
Del: bad code -- https://master.php.net/note/delete/94356/bad+code
Del: spam -- https://master.php.net/note/delete/94356/spam
Del: non-english -- https://master.php.net/note/delete/94356/non-english
Del: in docs -- https://master.php.net/note/delete/94356/in+docs
Del: other reasons-- https://master.php.net/note/delete/94356
Reject -- https://master.php.net/note/reject/94356
Search -- https://master.php.net/manage/user-notes.php