views:

242

answers:

1

Is there a function in PHP that can decode Unicode escape sequences like "\u00ed" to "í" and all other similar occurrences?

I found similar question here but is doesn't seem to work.

+3  A: 

Try this:

function replace_unicode_escape_sequence($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UTF-16BE');
}
$str = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $str);
Gumbo
Where do I put "\u00ed"?
Docstero
@Docstero: The regular expression will match any sequence of `\u` followed by four hexadecimal digits.
Gumbo
Warning: preg_replace_callback() [function.preg-replace-callback]: Compilation failed: PCRE does not support \L, \l, \N, \U, or \u at offset 1
Docstero
@Docstero: Fixed that.
Gumbo
Thanks a lot, works perfectly!:)
Docstero