October 10th, 2004, 03:11 PM
#1
Number of characters in a string
How can I count the number of characters in a UTF-8 encoded string with PHP version 3.23.58?
October 11th, 2004, 02:13 AM
#2
Re: Number of characters in a string
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?
October 11th, 2004, 04:59 AM
#3
Re: Number of characters in a string
PHP Code:
$string = 'six@lgfhfget@;][' ;
$string_count = strlen ( ereg_replace ( '[^[:alpha:]]+' , '' , $string ));
echo $string_count ;
October 11th, 2004, 05:16 AM
#4
Re: Number of characters in a string
Originally Posted by
azz0r
PHP Code:
$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)
October 11th, 2004, 03:54 PM
#5
Re: Number of characters in a string
Have a read of some of the comments here where they discuss the strlen function and UTF-8
September 11th, 2009, 01:44 AM
#6
Re: Number of characters in a string
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));
September 12th, 2009, 12:49 PM
#7
Re: Number of characters in a string
Welcome to the forums, pradeepgems.
Please remember to keep your posts relevant. This thread is 5 years old.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks