When I was just learning php, my friends often found a bunch of garbled characters in the content output in the browser, especially when outputting Chinese characters. Why are there garbled characters? In general, there are two possible causes for garbled text. One is that the encoding (charset) is incorrect, which leads to incorrect encoding for parsing by the browser. when my friends first learned php, it is often found that a bunch of garbled characters occur in the output content in the browser, especially when outputting Chinese characters. Why are there garbled characters? In general, there are two possible causes for garbled characters. One is that the encoding (charset) settings are incorrect, which leads to the browser parsing by wrong encoding, as a result, there is a mess of "books" on the full screen. The second is that the file is opened with an incorrect code and then saved. for example, a text file was originally GB2312 encoded, but to open the UTF-8 encoding and then save, there will be garbled problem. In the following article, let's talk about php Chinese garbled text and solutions.
Php Chinese garbled
1. how does php solve Chinese garbled characters?
First: Solution to Chinese garbled characters in HTML
If your HTML file is garbled, you can add UTF8 encoding (International code) in the head tag: UTF-8 is not country-specific code, that is, it can be used in any language independently.
Second, HTML and PHP hybrid page solution
Third, Chinese garbled characters on pure PHP pages (data is static)
Fourth, Chinese garbled characters in PHP + Mysql
In addition to the operations described in the third method, you must add the database encoding before you query, modify, and add data. Furthermore, it is worth noting that the UTF8 here is different from the previous one, and there is no horizontal line in the middle.
2. PHP's use of strrev to flip Chinese garbled characters
Header ("Content-Type: text/html; charset = utf-8"); $ str = 'processing garbled characters when using the strrev () function '; function cnstrrev ($ str) {$ len = strlen ($ str); for ($ I = 0; $ I <$ len; $ I ++) {$ char = $ str {0 }; if (ord ($ char)> 127) // The ord () function obtains the ASCII code of the first character. if it is greater than 0xa0 (127) it is a Chinese character {$ I + = 2; // UTF-8 encoding, a Chinese character occupies three bytes if ($ I <$ len) {$ arr [] = substr ($ str, 0, 3); // in UTF-8 encoding, a Chinese character occupies three bytes. $ str = substr ($ str, 3) ;}} else {$ arr [] = $ char; $ str = substr ($ str, 1); // otherwise it is not Chinese, occupies one character} return join (array_reverse ($ arr); // returns an array in reverse element order:} echo $ str. '+ '. cnstrrev ($ str );
3. php json_encode UTF-8 Chinese garbled solution
This simple code conversion, urlcode and then return the required array
public static function encodeOperations ($array) { foreach ((array)$array as $key => $value) { if (is_array($value)) { encodeOperations($array[$key]); } else { $array[$key] = urlencode(mb_convert_encoding($value,'UTF-8','GBK')); } } return $array; }
4. php url address bar with Chinese garbled characters
Url encoding
Syntax: string urlencode (string str );
Return value: string
Function type: encoding
Url decoding
Returns the URL encoded string.
Syntax: string urldecode (string str );
Return value: string
Function type: encoding
5. how to solve the problem of garbled characters between gbk and gb2312 in json_encode in php
It can be seen that UTF-8 characters are used, and Chinese garbled characters are also used in json_encode. The solution is to use the urlencode () function to process the characters before using json_encode, and then use the urldecode () function to return the output results.
6. solve the problem of Chinese garbled Ajax in PHP
Because XMLHttp decodes responstBody in UTF-8 encoding when processing returned responstText, if the data stream sent by the server is indeed UTF-8 encoding, the text will be correctly displayed, however, if it is GBK or other encoding, the above situation will occur.
Related Q &
1. encountered a magical problem caused by Chinese garbled characters.
2. php Chinese garbled characters
3. Chinese garbled characters on webpages cannot be solved, as shown in the figure below.