In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to solve the problem that php5.2 Json cannot handle Chinese and GB codes correctly." Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "How to solve the problem that php5.2 Json cannot correctly handle Chinese and GB codes"!
PHP 5.2's new json feature is very popular, but after testing,
json_encode has a problem with handling Chinese.
1. Cannot process GB codes, all GB codes will be replaced with null characters.
2. utf8 encoded Chinese is encoded into unicode encoding, equivalent to the result of javascript escape function processing.
The copy code is as follows:
/*
In order to use json correctly, we should first use utf8 encoding on the encoding, and then json_encode return results can be slightly processed to get the correct results.
I wrote a simple class that wraps these two functions,
**/
class Json{
public static function encode($str){
$code = json_encode($str);
return preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $code);
}
public static function decode($str){
return json_decode($str);
}
}
//when used
Json::encode($code);
Json::decode($code);
/** This can correctly process utf8 encoded Chinese.
PS: For GB encoded Chinese, we can convert to UTF8 encoding first, then encode, and then perform a utf8 -> gb conversion when decoding.
In addition, we usually return the result of json_encode to the client to use. In fact, we can also use javascript's unescape function to decode unicode encoded Chinese, thus restoring it to correct Chinese.
Or: $title = mb_convert_encoding($title, 'HTML-ENTITIES', $this->_outCharset);//Any encoding will display normally
********/
At this point, I believe that everyone has a deeper understanding of "how to solve the problem that php5.2 Json cannot correctly handle Chinese and GB codes." It may be useful to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.