How do you encode strings like \ u00d6?

This is encoded: \u00d6
It is decrypted: Ö

What function should I use to decode this string into something readable?

 \u00d6asdf -> Öasdf 
+6
php decode
source share
2 answers

This will usually be the urldecode method, but it does not apply to Unicode characters like yours. Try 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); } 
+2
source share

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 an escape function used in JSON, another option would be json_decode . This, however, also requires avoiding double quotes and backslashes before (except for those executed by \uXXXX escape sequences), and adding double quotes around the string. If, however, the string is indeed JSON encoded and that originally motivated the question, the correct answer would naturally be to use json_decode instead of the method described above.

+6
source share

All Articles