PHP decodes Unicode encoding with the text character code example,
After crawling a Web site data, the results found a string of encoded data in the packet: ".... \u65b0\u6d6a\u5fae\u535a ...", this is actually Chinese Unicode encoded after the data, want to decode the Chinese.
Solution:
Scenario A (Stable + recommended):
functionReplace_unicode_escape_sequence ($match) {returnMb_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//www.jbxue.com Script Academy//We will package the above scenario A to wrap up ~ ~ ~ (Plan a stable version + upgrade + recommended)classhelper_tool{Static functionUnicodedecode ($data){ functionReplace_unicode_escape_sequence ($match) {returnMb_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;} }//called$name= ' \u65b0\u6d6a\u5fae\u535a ';$data= Helper_tool::unicodedecode ($name);//output Sina Weibo
Tip: It's helpful to turn over the PHP tutorials abroad.
Scenario B (Recommended):
PHPfunction unicodedecode ($name) {$json = ' {' str ': '. $name. ' "} ' ; $arr = Json_decode ($json,true); if (empty($arrreturn 'return$arr[' str '];} /www.jbxue.com$name = ' \u65b0\u6d6a\u5fae\u535a '; echo unicodedecode ($name//
For scenario B, note that in the friend XAR (Poke XAR blog) technical support, summed up the string to be processed (that is, the parameters passed to the function Unicodedecode $name must not contain single quotation marks, otherwise it will lead to parsing failure, so if necessary, you can use str The _replace () function formats illegal characters as qualified characters)
http://www.bkjia.com/PHPjc/863580.html www.bkjia.com true http://www.bkjia.com/PHPjc/863580.html techarticle PHP decoding Unicode encoding in the text character code example, in the crawl of a Web site data, the result in the packet found a string of encoded data: "... ... \u65b0\u6d6a\u5fae\u535a ...", this is actually ...