Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is \u0026#39;n and how do I decode it?

Tags:

php

encoding

So I have a string that is in another language, most of it looks great, but parts of it is encoded incorrectly. How do I convert the literal string \u0026#39;n into its unicode(?) equivalent in PHP?

like image 441
rook Avatar asked Mar 14 '10 20:03

rook


1 Answers

The following PHP function will translate \u0026#39;n into 'n. This is used to communicate with the Google Translate API.

function unescapeUTF8EscapeSeq($str) {
    return preg_replace_callback("/\\\u([0-9a-f]{4})/i",
        create_function('$matches',
            'return html_entity_decode(\'&#x\'.$matches[1].\';\', ENT_QUOTES, \'UTF-8\');'
        ), $str);
}
like image 119
rook Avatar answered Oct 06 '22 11:10

rook