CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2004
    Location
    Richese
    Posts
    7

    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?

  2. #2
    Join Date
    Oct 2004
    Location
    Richese
    Posts
    7

    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?

  3. #3
    Join Date
    Oct 2004
    Posts
    2

    Re: Number of characters in a string

    PHP Code:
        $string 'six@lgfhfget@;][';
        
    $string_count strlen(ereg_replace('[^[:alpha:]]+'''$string));
        echo 
    $string_count

  4. #4
    Join Date
    Oct 2004
    Location
    Richese
    Posts
    7

    Re: Number of characters in a string

    Quote 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)

  5. #5
    Join Date
    Sep 2004
    Posts
    247

    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

  6. #6
    Join Date
    Feb 2008
    Location
    Indian
    Posts
    2

    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));

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    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
  •  





Click Here to Expand Forum to Full Width

Featured