SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP, json_encode and special characters?!?!

    I'm trying to figure this out...
    PHP Code:
    $users = array('Peter Jørgensen''Glen Thompson''Kit Anderson');
    $checkuser = array();

    foreach (
    $users as $user){
        
    $checkuser[] = array($user);
    }

    echo 
    json_encode($checkuser
    ;

    This prints out:
    [["Peter J\u00f8rgensen"],["Glen Thompson"],["Kit Anderson"]]

    As you can see the "Jørgensen" comes out like "J\u00f8rgensen"... How do I get it to show/print this correct???

    Please HELP!!!

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's just a unicode character code. Try using another encoding script, possibly the Zend one:

    http://stackoverflow.com/questions/4...hps-jsonencode
    Cross browser css bugs

    Dan Schulz you will be missed

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Read somewhere else that I could utf8_encode'ing the string, but not sure where, when and how... Please help!

  4. #4
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you try reading the comments on the php.net json_encode page?
    Cross browser css bugs

    Dan Schulz you will be missed

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmansa View Post
    As you can see the "Jørgensen" comes out like "J\u00f8rgensen"... How do I get it to show/print this correct???
    That is printing correctly. Anything that consumes JSON (JavaScript or any other client library like PHP's JSON extension) should handle those "special" (they're not really special, just not ASCII) characters without any problems.
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    FYI, I think you might have been asking about iconv.

  7. #7
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried to surf around the suggested websites and googled it on my own, but still can't get this to work...

    I have tried this:
    PHP Code:
    $users = array('Peter Jørgensen''Glen Thompson''Kit Anderson');
    $encodedUsers array_map(utf8_encode$users); 
    This only makes it like this:
    [["Peter J\u00c3\u00b8rgensen"],["Glen Thompson"],["Kit Anderson"]]

    If I use utf8_decode, then the first name comes out as ["null"]...

    Its tearing me apart... Please help... I am so close to cry :-|

  8. #8
    SitePoint Zealot
    Join Date
    Aug 2009
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As Salathe said this is just an encoding problem -

    see http://www.sitepoint.com/forums/showthread.php?t=647194

  9. #9
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmansa View Post
    As you can see the "Jørgensen" comes out like "J\u00f8rgensen"... How do I get it to show/print this correct???
    It is correct. Why do you want it to be different?

  10. #10
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    It is correct. Why do you want it to be different?
    Well... How do you mean it comes out correct???

  11. #11
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmansa View Post
    Well... How do you mean it comes out correct???
    It's in accordance the json specs. While it would also be allowed to print the characters directly, the php-extension chooses to encode non-ascii characters as their unicode points to ensure safe transport. Any standards-compliant json-parser will know how to interpret this.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •