3

Can I use unicode value of a character (for example U+0021 for !) in php? and convert it to original character in printing (with chr() or other functions)?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
mrdaliri
  • 7,148
  • 22
  • 73
  • 107
  • exact duplicate: http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-char – Alec Gorge Feb 20 '11 at 20:18

1 Answers1

2
function replace_unicode_escape_sequence($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}
function unicode_chr ($chr) {
    $x = explode("+", $chr);
    $str = "\u".end($x);
    return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $str);
}

var_dump(unicode_chr("U+0021")); // string(1) "!"

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

Community
  • 1
  • 1
Alec Gorge
  • 17,110
  • 10
  • 59
  • 71