Skip to content

Instantly share code, notes, and snippets.

@aeurielesn
Created July 31, 2011 03:47
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save aeurielesn/1116358 to your computer and use it in GitHub Desktop.
Save aeurielesn/1116358 to your computer and use it in GitHub Desktop.
Decode Unicode strings in PHP
<?php
#source: http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-char
function replace_unicode_escape_sequence($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}
function unicode_decode($str) {
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $str);
}
$str = unicode_decode('\u00e9');
?>
@nikhilavyatech
Copy link

Nice, really appreciable. Thanks

@alitayfur
Copy link

$str = utf8_decode(unicode_decode('\u00e9'));

nice Thanks for snipped 🗡️

@mjaning
Copy link

mjaning commented Jul 11, 2019

Great code, works like a charm. Many thanks!

@mehdichaouch
Copy link

Smallest way :

$str = "Ludwigstra\u00c3\u009fe 51";
echo utf8_decode(implode(json_decode('["' . $str . '"]')));

Demo : http://codepad.org/VYhNMlgc

@jihen-ben-said
Copy link

Many thanks

@mongmonk
Copy link

Smallest way :

$str = "Ludwigstra\u00c3\u009fe 51";
echo utf8_decode(implode(json_decode('["' . $str . '"]')));

Demo : http://codepad.org/VYhNMlgc

Thanks. simple worked like a charm :)

@a415Production
Copy link

Smallest way :

$str = "Ludwigstra\u00c3\u009fe 51";
echo utf8_decode(implode(json_decode('["' . $str . '"]')));

Demo : http://codepad.org/VYhNMlgc

Awesome! Thank you!

@olehtalanov
Copy link

utf8_decode is depricated.

@mehdichaouch
Copy link

utf8_decode is depricated.

You right @olehtalanov, this function has been DEPRECATED as of PHP 8.2.0, see details.

@mehdichaouch
Copy link

An alternative for PHP8.2 and above is:

$str = "Ludwigstra\u00c3\u009fe 51";
echo mb_convert_encoding(implode(json_decode('["' . $str . '"]')), 'ISO-8859-1', 'UTF-8');

Demo: https://onlinephp.io/c/19fe8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment