Once more a code snippet I'd like to find (again) to make life easier. This time, it's about decoding Unicode. Often you get strings with
Method 1: Decode using the json_decode
#
The json_decode
function does automatically convert Unicode strings:
$text = 'Elon \u2018Technoking\u2019 Musk';
echo json_decode('"' . $text . '"'); // prints "Elon 'Technoking' Musk"
You can wrap this in a helper function:
function unicode_decode($str) {
return json_decode('"' . $string . '"');
}
$text = 'Elon \u2018Technoking\u2019 Musk';
echo unicode_decode($text); // prints "Elon 'Technoking' Musk"
More details are on Stackoverflow.
Method 2: Unicode codepoint escape #
You can use a unicode escape syntax since PHP 7:
echo "\u{9999}"; // prints 香
Assuming you have a string containing \u9999
you can use a regex to add the required brackets.
You can find more information in the PHP docs.
🙏🙏🙏
Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.
Published