KitzKikz  KitzKikz: PhpUnicodeToUtf8   RecentChanges 
 PopularPages 
 SearchPages 
 Home | Trail - PhpUnicodeToUtf8
 
 

Convert Unicode Escaped String to PHP String

The various Google API calls return JSON strings containing things like "\u003c". These are escape sequences meant to be converted into single Unicode characters. Javascript does this automatically. PHP does not. I could not find a ready made function in PHP 5 to convert these into their equivalent UTF8 characters. So, below is what I came up with.


<?php

    $result preg_replace_callback('/\\\\u[0-9a-fA-F]{4}/','unescuni',$json);

    // $json is a string returned from a google api call 
    // More info can be found at: 
    //     http://code.google.com/apis/ajaxsearch/documentation/index.html#fonje_snippets_php

    function unescuni ($match) {
        return unicode2utf8(hexdec(substr($match[0],2)));
    }

    function unicode2utf8($c) {
        if($c 0x80) {
            return chr($c);
        }
        else if($c 0x800) {
            return chr0xc0 | ($c >> 6) )
            . chr0x80 | ($c 0x3f) );
        }
        else if($c 0x10000) {
            return chr0xe0 | ($c >> 12) )
            . chr0x80 | (($c >> 6) & 0x3f) )
            . chr0x80 | ($c 0x3f) );
        }
        else if($c 0x200000) {
            return chr(0xf0 | ($c >> 18))
            . chr(0x80 | (($c >> 12) & 0x3f))
            . chr(0x80 | (($c >> 6) & 0x3f))
            . chr(0x80 | ($c 0x3f));
        }
        return false;
    }
?>


 

   

 
 EditThisPage · LinksToPage · PageInfo 10/02/09 21:43:34  ·  0.0555s