This article mainly introduces the PHP decoding Unicode encoding, has a certain reference value, now share to everyone, the need for friends can refer to the
function Unicode_decode ($name) {//method one $name = Str_replace ("\\\\u", "\u", $name); $json = ' {' str ': '. $name. '} '; $arr = Json_decode ($json, true); if (empty ($arr)) return '; return $arr [' str ']; Method two//convert encoding to convert Unicode encoding into a utf-8 encoding that can be browsed $pattern = '/([\w]+) | (\\\u ([\w]{4}))/I '; Preg_match_all ($pattern, $name, $matches); if (!empty ($matches)) {$name = '; for ($j = 0; $j < count ($matches [0]), $j + +) {$str = $matches [0][$j]; if (Strpos ($str, ' \\u ') = = = 0) {$code = Base_convert (substr ($STR, 2, 2), 16, 10); $code 2 = Base_convert (substr ($STR, 4), 16, 10); $c = Chr ($code). Chr ($code 2); $c = Iconv (' UCS-2 ', ' UTF-8 ', $c); $name. = $c; } else {$name. = $str; }}} return $name; }
here's how to find it online:
Scenario A (Stable + recommended):
function Replace_unicode_escape_sequence ($match) { return mb_convert_encoding (Pack (' h* ', $match [1]), ' UTF-8 ', ' Ucs-2be ');} $name = ' \u65b0\u6d6a\u5fae\u535a '; $str = Preg_replace_callback ('/\\\\u ([0-9a-f]{4})/I ', ' replace_unicode_escape_ Sequence ', $name); Echo $str; Output: Sina Weibo
Let's wrap up the above scenario a (Plan a stable + upgrade + recommended) class helper_tool{ static function Unicodedecode ($data) { function Replace_unicode_escape_sequence ($match) { return mb_convert_encoding (Pack (' h* ', $match [1]), ' UTF-8 ', ' ucs-2be ') ; } $rs = Preg_replace_callback ('/\\\\u ([0-9a-f]{4})/I ', ' replace_unicode_escape_sequence ', $data); return $rs; } } Call $name = ' \u65b0\u6d6a\u5fae\u535a '; $data = Helper_tool::unicodedecode ($name); Output Sina Weibo
Scenario B (Recommended):
<?phpfunction Unicodedecode ($name) {$json = ' {' str ': ' '. $name. '} '; $arr = Json_decode ($json, true); if (empty ($arr)) return '; return $arr [' str '];} $name = ' \u65b0\u6d6a\u5fae\u535a '; Echo unicodedecode ($name); Output: Sina Weibo