Strlen on UTF-8 Strings May 8, 2006
Posted by rossoft in CakePHP.trackback
If you want to know the length of a UTF-8 string, strlen doesn’t work. You must do a little trick for it:
function utf8_strlen($str)
{
return strlen(utf8_decode($str));
}
It’s safer and better to use “mb_strlen”. Just remember to set the encoding with “mb_internal_encoding” first.
mb_strlen is from the extension “Multibyte String”. mbstring is a non-default extension
You can also check for the existence of mb_strlen
function utf8_strlen($str)
{
if(function_exists(’mb_strlen’))
return mb_strlen($str);
return strlen(utf8_decode($str));
}
Very simple & very usefull
$len = strlen(utf8_decode($string));
strtolower - also problem with UTF-8 non roman cahrs …