note 72979 added to function.mb-strlen
| From: | motin at demomusic dot nu | Date: | Tue, 06 Feb 2007 18:12:51 +0000 |
| Subject: | note 72979 added to function.mb-strlen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122932@lists.php.net to get a copy of this message | ||
Thank you Peter Albertsson for presenting that!
After spending more than eight hours tracking down two specific bugs in my mbstring-func_overloaded
environment I have learned a very important lesson:
Many developers rely on strlen to give the amount of bytes in a string. While mb-overloading has
very many advantages, the most hard-spotted pitfall must be this issue.
Two examples (from the two bugs found earlier):
1. Writing a string to a file:
<?php
$str = "string with utf-8 chars åèä - doo-bee doo-bee dooh";
$fp = fopen($this->_file, "wb");
if ($fp) {
$len = strlen($str);
fwrite($fp, $str, $len);
}
?>
PS This is found i the PEAR::Cache_Lite package (Lite.php) - Reported
2. Iterating through a string's characters:
<?php
$str = "string with utf-8 chars åèö - doo-bee doo-bee dooh";
$newStr = "";
for ($i = 0; $i < strlen($str); $i++) {
$newStr .= $str[$i];
}
?>
Both of these situations will fail to save / store the last characters in $str. This can be very
hard to spot and can be especially fatal for say serialized strings, xml etc.
So, try to avoid these situations to support overloaded environments, and remeber Peter Albertssons
remark if you find problems under such an environment.
----
Server IP: 84.47.204.3
Probable Submitter: 213.114.132.137
----
Manual Page -- http://www.php.net/manual/en/function.mb-strlen.php
Edit -- https://master.php.net/note/edit/72979
Del: integrated -- https://master.php.net/note/delete/72979/integrated
Del: useless -- https://master.php.net/note/delete/72979/useless
Del: bad code -- https://master.php.net/note/delete/72979/bad+code
Del: spam -- https://master.php.net/note/delete/72979/spam
Del: non-english -- https://master.php.net/note/delete/72979/non-english
Del: in docs -- https://master.php.net/note/delete/72979/in+docs
Del: other reasons-- https://master.php.net/note/delete/72979
Reject -- https://master.php.net/note/reject/72979
Search -- https://master.php.net/manage/user-notes.php