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

You are looking for mb_strlen.


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).

EDIT

If for any reason change in code is not possible/doable, one may want to ensure string functions are automatically overloaded by multi-byte counterparts:

To use function overloading, set mbstring.func_overload in php.ini to a positive value that represents a combination of bitmasks specifying the categories of functions to be overloaded. It should be set to 1 to overload the mail() function. 2 for string functions, 4 for regular expression functions. For example, if it is set to 7, mail, strings and regular expression functions will be overloaded.

This is supported by PHP and documented here (note this feature is deprecated in PHP 7.2 and newer).

Please note that you may also need to edit your php.ini to ensure mb_string module is enabled. Available settings are documented here.


Function strlnen does not count the number of characters, but the number of bytes. For multibyte characters it will return higher numbers.
Use mb_strlen() instead to count the actual count of characters.

Tags:

Php

Strlen