Use mb_strlen
Returns the number of characters in str string that have character encoding (second parameter). A multibyte character is considered equal to 1.
Since your 3 characters are multibyte, you get 6 returned with strlen , but that returns 3 as expected.
echo mb_strlen($string,'utf-8');
Fiddle
Note
It is important not to underestimate the power of this method and any such alternatives. For example, one could say “good” if the characters are multi-byte, and then just get the length using strlen and divide it by 2, but this will only work if all the characters in your string are multi-byte and even period . will invalidate the account. For example, this
echo mb_strlen('علی.','utf-8');
Returns 4 , which is correct. Thus, this function not only takes the entire length and divides it by 2, it takes into account 1 for each multi-byte character and 1 for each single-byte character.
Note2:
It seems that you decided not to use this method, because the mbstring is not used by default for older versions of PHP, and you may have decided not to try to enable it :) For future readers, this is not difficult, and it is recommended to include it if you are dealing with multibyte characters, as this is not only the length with which you may be required. See manual
Hanky panky
source share