High performance and simple implementation scheme of PHP character special character UNICODE encoding and decoding

Source: Internet
Author: User
Tags php unicode

Online, for Chinese characters, special characters, Unicode encoding, decoding implementation, many methods, but mostly copy and paste, no new ideas!

PHP UNICODE Chinese Character coding:
Var_dump (Json_encode (' 2018 ABC I'm Chinese! Website: http://my.oschina.net/cart/'));
The above implementation of PHP in the Chinese characters, special characters UNICODE encoding!

How easy!

The above outputs:

String "" "2018 ABC \u6211\u662f\u4e2d\u56fd\u4eba\uff01\u7f51\u7ad9\uff1ahttp:\/\/my.oschina.net\/cart\/" "
PHP UNICODE Kanji decoding: if we try to


Var_dump (Json_decode (' 2018 ABC \u6211\u662f\u4e2d\u56fd\u4eba\uff01\u7f51\u7ad9\uff1ahttp:\/\/my.oschina.net\/ cart\/'));
Or


Var_dump (Json_decode ("2018 ABC \u6211\u662f\u4e2d\u56fd\u4eba\uff01\u7f51\u7ad9\uff1ahttp:\/\/my.oschina.net\/ cart\/"));
The results are all
Null
Is wrong and is not decoded according to our wishes!


Because you did not pass in the correct JSON format string !!!

Cautious's classmates must have found that the above json_encode output is

"2018 ABC \u6211\u662f\u4e2d\u56fd\u4eba\uff01\u7f51\u7ad9\uff1ahttp:\/\/my.oschina.net\/cart\/"
Note that the output string is two paragraphs Double quotes,This is also the output Json_encode part of the content! It's important!


So if we want to use Json_decode decoding correctly, we can do the following:

Var_dump (Json_decode (' "2018 ABC \u6211\u662f\u4e2d\u56fd\u4eba\uff01\u7f51\u7ad9\uff1ahttp:\/\/my.oschina.net\/ cart\/"'));
Haha, the correct decoding output of our results:


String (+) "2018 ABC I'm Chinese!" Website: http://my.oschina.net/cart/"


See, there is no technical content, are the basic knowledge of the master! But why do people think a lot of wonderful ways to decode it? Basic skills are not solid, you can only impossible, even if achieved, the final performance is very poor!

Some use pack, some use Utf8_decode, mb_convert_encoding, Iconv ...


PHP UNICODE Chinese characters are decoded in irregular form:


Maybe some of the cock silk is not so lucky, get the string shape like:


2018 ABC u6211u662fu4e2du56fdu4ebauff01u7f51u7ad9uff1ahttp:\/\/my.oschina.net\/cart\/
How to decode? How did it break?


Cautious colleagues have found that all u less \, should be \u,u followed by the "0-9a-f" 4-bit string!

Swaiiow Chic (preg_replace e modifier in the new version of PHP has been deprecated , so the official recommended to use Preg_replace_callback instead of! ):

$str = ' 2018 ABC u6211u662fu4e2du56fdu4ebauff01u7f51u7ad9uff1ahttp:\/\/my.oschina.net\/cart\/'; Var_dump (json_ Decode (' "'. Preg_replace_callback ("/U ([0-9a-f]{4})/", function ($match) {return ' \u '. $match [1];}, $STR).



Just 1 words, how chic! Green pollution-free, suitable for all ages! Deep Work and name!

Thank you, http://my.oschina.net/cart/, for the most elegant way to solve the problem. High performance and simple implementation scheme of PHP character special character UNICODE encoding and decoding

High performance and simple implementation scheme of PHP character special character UNICODE encoding and decoding

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.