Php converts data in this form into Chinese characters, which will lose part of the characters and numbers. how can this problem be solved?

Source: Internet
Author: User
Php converts data in this form into Chinese characters, which will lose part of the characters and numbers and find a solution to transcoding. $ Code & nbsp ;=& nbsp; xml_to_json ($ code); after conversion using this function, characters such as Chinese characters will become the following u6c5b \ u671f \ u5c3e \ u58f0 \ u7684 \ u5230 \ u php converts data in this form into Chinese characters, and some characters and numbers will be lost, transcoding solution.
$ Code = xml_to_json ($ code );
After conversion using this function, Chinese characters and other characters will become the following:
Alibaba \ u671f \ u5c3e \ u58f0 \ u7684 \ u5230 \ u6765 \ Alibaba \ u6211 \ u7701 \ Alibaba \ u59cb \ Alibaba \ u5229 \ Alibaba \ u8bbe \ u7684 \ u9ec4 \ u91d1 \ u65f6 \ Alibaba \ u3002 \ u8bb0 \ u80058 \ Alibaba \ u65e5 \ u83b7 \ u6089 \ Alibaba \ u4eca \ Alibaba \ u6211 \ u7701 \ u5c06 \ Alibaba \ u6210 \ Alibaba \ u5229 \ Users \ u4ebf \ u5143 \ uff0c \ u91cd \ u70b9 \ u6db5 \ Users \ u9762 \ Users \ u4e2a \ u9879 \ u76ee \ u3002 <\/ p> \ n


I now use the following function to convert the above to Chinese characters, but some characters and numbers will be lost ,.

$code = preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $code);
print $code;


Share:
------ Solution --------------------
$ Code = preg_replace_callback ('/\\\\ u ([0-9a-f] {4})/I', create_function ('$ matches ', 'return mb_convert_encoding (pack ("H *", $ matches [1]), "UTF-8", "UCS-2BE"); '), $ str );
You can modify this
------ Solution --------------------
header('Content-type: text/html;charset=utf-8');
$code =<<< TXT
\u6c5b\u671f\u5c3e\u58f0\u7684\u5230\u6765\uff0c\u6211\u7701\u5f00\u59cb\u8fdb\u5165\u6c34\u5229\u5efa\u8bbe\u7684\u9ec4\u91d1\u65f6\u671f\u3002\u8bb0\u80058\u670826\u65e5\u83b7\u6089\uff0c\u4eca\u5e74\uff0c\u6211\u7701\u5c06\u5b8c\u6210\u6c34\u5229\u6295\u8d44\u8fbe154\u4ebf\u5143\uff0c\u91cd\u70b9\u6db5\u76d616\u4e2a\u65b9\u9762\u51713286\u4e2a\u9879\u76ee\u3002

\n\n
TXT;
echo preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $code);
echo preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $code);
At the end of the flood season, our province began to enter the golden period of water conservancy construction. The reporter learned on the day of the month that this year, our province will complete the water conservancy investment of hundreds of millions of yuan, focusing on a total of projects.



At the end of the flood season, our province began to enter the golden period of water conservancy construction. This year, the reporter learned that our province will complete water conservancy investment of 15.4 billion yuan, focusing on 16 aspects of a total of 3286 projects.



The reason for the loss of characters and numbers is that you swallowed up redundant characters. unicode is expressed in 4-digit hexadecimal notation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.