tags:

views:

92

answers:

2

This is encoded: \u00d6
This is decoded: Ö

What function I have to use to decode that string into something readable?

\u00d6asdf -> Öasdf
+2  A: 

Normally this would be the urldecode method, but it does not apply to unicode characters, like yours. Try this one instead:

function unicode_urldecode($url)
{
   preg_match_all('/%u([[:alnum:]]{4})/', $url, $a);

   foreach ($a[1] as $uniord)
   {
       $utf = '&#x' . $uniord . ';';
       $url = str_replace('%u'.$uniord, $utf, $url);
   }

   return urldecode($url);
} 
Hinek
There are several things wrong here... You substitute %uXXXX instead of \uXXXX, replace the sequences with entities (which would be fine for displaying the string, except now you have to tell htmlspecialchars not to double encode the string and of course, now you don't distinguish html entitites that are originally in the string for the generated ones); finally you call urldecode which has nothing to do with the question -- url encoding encodes bytes (max value is %FF) and it certainly doesn't decode the html entities you just created.
Artefacto
+3  A: 

To convert to UTF-8, do:

preg_replace('/\\\\u([0-9a-f]{4})/ie',
    'mb_convert_encoding("&#x$1;", "UTF-8", "HTML-ENTITIES")',
    $string);

Since this is the escaping used in JSON, another option would be json_decode. This would, however, also require escaping double quotes and backslashes before (except those of the \uXXXX escape sequences) and adding double quotes around the string. If, however, the string is indeed JSON-encoded and that's what originally motivated the question, the correct answer would naturally be use json_decode instead of the method above.

Artefacto