PHP decode Unicode string

Given the Unicode codepoint in hexadecimal form:


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.


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) "á"

Please support this site and join our Discord!