PHP Snippet: How to decode unicode in PHP strings (`ሴ`)

Here is a PHP snippet that can make your (and future-me's) life easier. In PHP, there are several methods for converting Unicode strings to UTF-8. One popular approach is to use PHP built-in functions like mbstring or iconv. In this article, we'll explore two methods for converting Unicode strings to UTF-8 in PHP:

With this, let's dive into code:

Method 1: Decode using the json_decode #

The json_decode function can automatically convert Unicode strings in PHP:

$text = 'Elon \u2018Technoking\u2019 Musk';

echo json_decode('"' . $text . '"'); // prints "Elon 'Technoking' Musk"

You can also create a helper function for this:

function unicode_decode(string $text): string {
return json_decode('"' . $text . '"');

$text = 'Elon \u2018Technoking\u2019 Musk';

echo unicode_decode($text); // prints "Elon 'Technoking' Musk"

For more information, please refer to this Stackoverflow post.

Method 2: Decoding using Unicode Encoding in PHP 7+ #

Since PHP 7.0, you can use a Unicode escape syntax to represent code points:

echo "\u{9999}";  // represents a unicode char.

If you have a string that contains \u9999, you can use a regular expression to add the required brackets:

// The string with Unicode
$text = 'Elon \u2018Technoking\u2019 Musk';

// The regular expression to match Unicode code point escapes
$regex = '/\\\\u([0-9a-fA-F]{4})/';

// Replace the Unicode code point escapes with their corresponding UTF-8 characters
$decoded = preg_replace_callback($regex, function ($match) {
// Convert the HTML entity (`&#xXXXX;`) to its UTF-8 character equivalent.
return mb_convert_encoding('&#x' . $match[1] . ';', 'UTF-8', 'HTML-ENTITIES');
}, $text);

echo $decoded; // Outputs "Elon ‘Technoking’ Musk"

For more information, please refer to the PHP documentation.

Again, you can create a helper function as shown above. If you are wondering about whether to use a Snippet such as this or not, you might find my considerations on the VanillaPHP vs. using packages of interest.


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.