Utf8 string length

strlen() Function in php could not correctly return utf8 string length, for example Ψ³Ω„Ψ§Ω… is 4 char, but after using strlen thats will return 8 chr

 <?php echo strlen('Ψ³Ω„Ψ§Ω…'); ?> 
+4
source share
2 answers

The basic functions of a PHP string all take 1 character = 1 byte. They have no concept of different encodings. To find out how many characters are in a UTF-8 string (not so many bytes), use the equivalent of mb_strlen and tell what encoding the string is in:

 echo mb_strlen('Ψ³Ω„Ψ§Ω…', 'UTF-8'); 
+13
source

You can get the number of UTF-8 code points inside a PHP binary string (provided that it is valid in UTF-8 encoding) ( Demo ):

 $length = preg_match_all('(.)su', $subject); 

You can also use the multibyte extension if it is installed:

 $length = mb_strlen($subject, 'UTF-8'); 

See also: PHP UTF-8 String

+3
source

Source: https://habr.com/ru/post/1447681/


All Articles