note 93918 deleted from function.substr by danbrown
| From: | danbrown@php.net | Date: | Tue, 06 Oct 2009 21:34:11 +0000 |
| Subject: | note 93918 deleted from function.substr by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161415@lists.php.net to get a copy of this message | ||
Note Submitter: marcus at mailinator dot com
----
Tested several suggested replacements for substr:
$start = 0;
$len = 15;
$str = "1234567890ô234567890";
echo "substr: [".substr($str,$start,$len)."]";
echo "<br>";
echo "preg_replace:
[".preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $start
.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $len
.'}).*#s','$1', $str)."]";
echo "<br>";
preg_match_all("/./su", $str, $ar);
echo "array_slice: [".join("",array_slice($ar[0],$start,$len))."]";
echo "<br>";
echo "mb_substr: [".mb_substr($str,$start,$len)."]";
When the above file has been saved in ANSI:
substr: [1234567890ô2345]
preg_replace: [1234567890]
array_slice: []
mb_substr: [1234567890ô2345]
When the above file has been saved in UTF-8:
substr: [1234567890ô234]
preg_replace: [1234567890ô2345]
array_slice: [1234567890ô2345]
mb_substr: [1234567890ô234]
So be careful which encoding while saving the file.
Anyone has a fix that works for ANSI and UTF-8 files?