How can I count the number of characters in a UTF-8 encoded string with PHP version 3.23.58?
Printable View
How can I count the number of characters in a UTF-8 encoded string with PHP version 3.23.58?
I am using strlen(), with a converted string using utf8_decode(). Always supposing that this function converts unknown characters as single quotation marks '?'. This is enough for counting characters purpose.
However, is there a more appropiate method?
PHP Code:$string = 'six@lgfhfget@;][';
$string_count = strlen(ereg_replace('[^[:alpha:]]+', '', $string));
echo $string_count;
Quote:
Originally Posted by azz0r
Thank you, but is not working.
It returns 0 length for UTF-8 entries like this: 後味 (it should return 2)
Have a read of some of the comments here where they discuss the strlen function and UTF-8
first of all you need to decode the UTF 8 encoded string using utf8_decode() and then use strlen() because UTF8 encoded may have more than one byte in one symbol.
your php code will be like this :
echo strlen(utf8_decode($string));
Welcome to the forums, pradeepgems. :wave:
Please remember to keep your posts relevant. This thread is 5 years old.