Skip to content

How to decode Unicode escape sequences like "\u00ed" to proper UTF-8 encoded characters?

In PHP, double-quoted strings do not natively interpret \uXXXX escape sequences, so the string contains a literal backslash followed by u. To convert a string containing these literal sequences into proper UTF-8 characters, you can use the json_decode() function as a workaround.

Here's an example:

Example of using the json_decode() function to decode a string that contains Unicode escape sequences in PHP

php
<?php

// Define a string with a literal \uXXXX escape sequence
$unicode_string = "caf\u00ed";

// Wrap in quotes and pass to json_decode to parse the escape sequence
$utf8_string = json_decode('"' . $unicode_string . '"');

// Check for decoding errors
if (json_last_error() === JSON_ERROR_NONE) {
    echo $utf8_string; // Output: café
}

This workaround relies on json_decode() expecting valid JSON syntax, which is why the string is wrapped in double quotes. The function correctly interprets \uXXXX sequences and returns the proper UTF-8 encoded character. For PHP 7.3+, you can pass the JSON_THROW_ON_ERROR flag to automatically throw a JsonException on failure, simplifying error handling.

Dual-run preview — compare with live Symfony routes.