In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will give you a detailed explanation on how to solve the problem of Chinese garbled codes in php transmitted from ajax to the background. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Ajax to the background php Chinese garbled solution: 1, set "iconv ('utf-8','gb2312',$_POST [' Data']);"; 2, through arrayRecursive and JSON methods to solve the coding problem.
This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer.
The solution of Chinese garbled Code in AJAX Application in PHP
Chinese garbled often occurs in web application development!
Today, the main ideas are as follows:
Most cases of garbled code are encountered when we are doing AJAX! Because direct database reading won't have any coding problems.
1. In the case of submitting data from the foreground JS to the background
When AJAX data, whether it is directly using the underlying xmlHttprequest or using jQuery's ajax for data transfer! ajax will use utf-8 to encode and send!
If our page is UTF-8, that's no problem! The past Chinese data will not be garbled!
If it is GB2312, then there will be a problem! GBK is converted by UTF-8 and sent to the background, when the backend directly received the garbled code!
(solution: if the background language is PHP, iconv ('utf-8','gb2312',$_POST [' Data'])
The principle is simple: utf-8 is directly received in the background, and the pages are all GBK, so as long as the received data is converted back to GBK!
At this time to do the database operation on it! The characters stored in the database must be normal Chinese characters!
two。 The background page reads that the data in the database contains Chinese characters, and then sends it back to the JS foreground.
The PHP page reads the normal Chinese characters from the database and wants to send them back through AJAX.
If you pass it back, you just send the data back in the form of a string without any processing! There is no problem with this! Everything is normal in Chinese on the front page!
But we usually need to package the background data! Otherwise, the data received by the front desk will be very difficult to deal with!
If it's PHP! So now we usually use JSON to pass on! In this way, the data passed to the foreground JS will be very easy to unlock and operate!
When performing json conversion operations in the PHP background, there is a problem!
During json conversion, you will find NULL or other abnormal conditions in the Chinese read from the database. This is because JSON only escapes encoding encoding! Let's not care what code he escapes!
Anyway, there is garbled code when JSON translates into Chinese ~ ~
The solution is as follows:
Function arrayRecursive (& $array, $function, $apply_to_keys_also = false) {static $recursive_counter = 0; if (+ + $recursive_counter > 1000) {die ('possible deep recursion attack');} foreach ($array as $key = > $value) {if (is_array ($value)) {arrayRecursive ($array [$key], $function, $apply_to_keys_also) } else {$array [$key] = $function ($value);} if ($apply_to_keys_also & & is_string ($key)) {$new_key = $function ($key); if ($new_key! = $key) {$array [$new_key] = $array [$key]; unset ($array [$key]) } $recursive_counter--;} function JSON ($array) {arrayRecursive ($array, 'urlencode', true); $json = json_encode ($array); return urldecode ($json);} $json_string=JSON ($data); / / $obj = json_decode ($json_string); print_r ($json_string)
After these processing, you will find that the Chinese characters read by the background PHP from the database have been successfully converted to json format and the Chinese language shows that everything is normal!
Of course, at this time, the data received by the front desk is in JSON format and the Chinese characters are normal!
All right, these are the ideas. There are many online methods! Look a little messy ~ mainly to remember the train of thought!
Then it depends on what function and what method to solve the related coding problem!
More annoying that kind of JS receptionist AJAX needs to transcode! Decode it backstage! Some need to turn twice!
What I have written so far is valid in the case of PHP background language code.
Even the foreground GBK does not need to do any transcoding operation, all only in the background PHP code to do the relevant conversion on it!
This is the end of the article on "how to solve the problem of Chinese garbled codes in php from ajax to the background". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.