Click to See Complete Forum and Search --> : Number of characters in a string


Tleilaxu
October 10th, 2004, 03:11 PM
How can I count the number of characters in a UTF-8 encoded string with PHP version 3.23.58?

Tleilaxu
October 11th, 2004, 02:13 AM
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?

azz0r
October 11th, 2004, 04:59 AM
$string = 'six@lgfhfget@;][';
$string_count = strlen(ereg_replace('[^[:alpha:]]+', '', $string));
echo $string_count;

Tleilaxu
October 11th, 2004, 05:16 AM
$string = 'six@lgfhfget@;][';
$string_count = strlen(ereg_replace('[^[:alpha:]]+', '', $string));
echo $string_count;



Thank you, but is not working.

It returns 0 length for UTF-8 entries like this: 後味 (it should return 2)

Davey
October 11th, 2004, 03:54 PM
Have a read of some of the comments here (http://uk2.php.net/strlen) where they discuss the strlen function and UTF-8

pradeepgems
September 11th, 2009, 01:44 AM
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));

PeejAvery
September 12th, 2009, 12:49 PM
Welcome to the forums, pradeepgems. :wave:

Please remember to keep your posts relevant. This thread is 5 years old.