PHP decode Unicode string


Given the Unicode codepoint in hexadecimal form:

\u00e1

How do you decode the Unicode string in PHP?

Unicode codepoint escape syntax

You can use Unicode codepoint escape syntax if you’re able to create the string:

echo "\u{00e1}"; // á

As you can see, the characters after \u must be warpped in curly braces. Also, the string must be double-quoted.

json_decode

Alternatively, you can use json_decode:

echo json_decode('"\u00e1"'); // á

This is useful for decoding dynamic strings:

$string = '\u00e1';
var_dump(json_decode('"' . $string . '"')); // string(2) "á"


If you enjoyed this post, please consider supporting this site!