note 59258 deleted from function.strlen by danbrown
| From: | danbrown@php.net | Date: | Thu, 12 Feb 2009 03:16:42 +0000 |
| Subject: | note 59258 deleted from function.strlen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150540@lists.php.net to get a copy of this message | ||
Note Submitter: anpaza at mail dot ru
----
Here's a better strlen() for UTF-8 strings that doesn't access the byte past end of the
string (on which newer PHP barfs):
function strlen_utf8 ($str)
{
$i = 0;
$count = 0;
$len = strlen ($str);
while ($i < $len)
{
$chr = ord ($str[$i]);
$count++;
$i++;
if ($i >= $len)
break;
if ($chr & 0x80)
{
$chr <<= 1;
while ($chr & 0x80)
{
$i++;
$chr <<= 1;
}
}
}
return $count;
}