note 93918 added to function.substr
| From: | marcus at mailinator dot com | Date: | Tue, 06 Oct 2009 21:31:32 +0000 |
| Subject: | note 93918 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-161414@lists.php.net to get a copy of this message | ||
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?
----
Server IP: 208.69.120.35
Probable Submitter: 80.139.116.52
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/93918
Del: integrated -- https://master.php.net/note/delete/93918/integrated
Del: useless -- https://master.php.net/note/delete/93918/useless
Del: bad code -- https://master.php.net/note/delete/93918/bad+code
Del: spam -- https://master.php.net/note/delete/93918/spam
Del: non-english -- https://master.php.net/note/delete/93918/non-english
Del: in docs -- https://master.php.net/note/delete/93918/in+docs
Del: other reasons-- https://master.php.net/note/delete/93918
Reject -- https://master.php.net/note/reject/93918
Search -- https://master.php.net/manage/user-notes.php