PHP to convert data into Chinese characters will lose some of the characters and numbers of the solution

Source: Internet
Author: User
PHP to convert data into Chinese characters will lose some of the characters and numbers of the solution

$code = Xml_to_json ($code);

After this function is converted, characters such as Kanji will become the following

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

Reply to discussion (solution)

1. You can try this:

$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);

2.

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</p>\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. Reporter learned that this year, the province will complete the Water conservancy investment billion, focusing on a total of a project. </P>
At the end of the flood season, our province began to enter the golden period of water conservancy construction. Reporter August 26 learned that this year, the province will complete the water investment of 15.4 billion yuan, focusing on 16 aspects of a total of 3,286 projects. </P>
The reason for the loss of characters and numbers is that you devour the extra characters, Unicode only in 4-bit hexadecimal

Thank you very much, the moderator, has done ... Moderator is a kinky only ah ...

$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);

The above is the PHP to convert the data into Chinese characters will lose some of the characters and numbers of the solution to the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.