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

php, strlen

Solution

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

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

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

Problem

I am trying to get the length of this unicode characters string ``` $text = 'نام سلطان م'; $length = strlen($text); echo $length; ``` output ``` 20 ``` How it determines the length of unicode characters string?

Original source