jump to navigation

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));
}

Comments»

1. Christian Davén - May 18, 2006

It’s safer and better to use “mb_strlen”. Just remember to set the encoding with “mb_internal_encoding” first.

2. rossoft - May 18, 2006

mb_strlen is from the extension “Multibyte String”. mbstring is a non-default extension :(

3. Daniel Alexandrov - May 23, 2006

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));
}

4. GelaMu - August 4, 2006

Very simple & very usefull :)
$len = strlen(utf8_decode($string));

5. majna - August 30, 2006

strtolower - also problem with UTF-8 non roman cahrs …