Munib Munib - 1 year ago 139
PHP Question

strlen() php function giving the wrong length of unicode characters

I am trying to get the length of this unicode characters string

$text = 'نام سلطان م';
$length = strlen($text);
echo $length;



How it determines the length of unicode characters string?

Answer Source

strlen() is not handling multibyte characters correctly, as it assumes 1 char equals 1 byte, which is simply invalud for unicode. This behavior is documented here:

strlen() returns the number of bytes rather than the number of characters in a string.

Solution is to use mb_strlen() function instead (mb stands for multi byte) (see mb_strlen() docs).


If for any reason chanage in code is not possible/doable, one may want to ensure string functions are automatically overloaded by multibyte counterparts. This is supported by PHP and documented here.

Please note that you may also want to edit your php.ini to ensure mb_string works as you want it to. Available settings are documented here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download