Php strlen () function specifying the wrong length of unicode characters

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

$text = 'Ω†Ψ§Ω… Ψ³Ω„Ψ·Ψ§Ω† Ω…';
$length = strlen($text);
echo $length;

Output

20

How does it determine the length of a string of Unicode characters?

+5
source share
4 answers

strlen()doesn't handle multibyte characters correctly, since it assumes 1 char is 1 byte, which is just not valid for unicode. This behavior is described here: http://php.net/strlen

strlen () returns the number of bytes , not the number of characters in a string.

The solution is to use the function mb_strlen()( mbmeans multi byte) ( see mb_strlen () docs ).

EDIT

- /, , . PHP .

, php.ini, , mb_string , . .

+19
+2

strlnen , . .
mb_strlen() , .

0

, , mb_strlen():

If the parameter php.in mbstring.func_overloadhas bit 2 set to 1, it strlenwill read characters based on the default encoding; otherwise it will count the number of bytes in the string

0
source

All Articles