Home  >  Article  >  Backend Development  >  Summary of solutions to the problem of php Chinese garbled characters

Summary of solutions to the problem of php Chinese garbled characters

伊谢尔伦
伊谢尔伦Original
2017-06-07 16:13:231933browse

When friends first learn PHP, they often find that the content output in the browser will have a lot of garbled characters, especially when outputting Chinese. Why are there garbled codes? Generally speaking, there are two reasons for the appearance of garbled characters. One is due to the incorrect encoding (charset) setting, which causes the browser to parse with the wrong encoding, resulting in a screen full of messy "heavenly books". The second is that the file is If you open it with the wrong encoding and then save it, for example, if a text file is originally encoded in GB2312 but is opened in UTF-8 encoding and then saved, garbled characters will appear. In the following article, we will talk about the problem and solution of Chinese garbled characters in PHP.

php Chinese garbled code problem

1. How to solve the Chinese garbled code problem in php?

First method: Solve the problem of Chinese garbled characters in HTML

If your HTML file has garbled characters, then you can add UTF8 encoding (internationalization) in the head tag Encoding): UTF-8 is a country-less encoding, that is, it is independent of any language and can be used in any language.

Second, HTML and PHP mixed page solution

Third, Chinese garbled problem of pure PHP page (the data is static)

Fourth , PHP+Mysql Chinese garbled problem

In addition to following the third operation, you must also add database encoding before querying/modifying/adding your data. Moreover, it is worth noting that the UTF8 here is different from the previous one, there is no horizontal line in the middle.

2. Solution to the problem of using strrev to flip Chinese garbled characters in PHP

header("Content-Type: text/html; charset=utf-8");
$str = '处理使用strrev()函数时的乱码问题';
function cnstrrev($str)
{
  $len = strlen($str);
  for($i = 0; $i < $len; $i++)
  {
    $char = $str{0};
    if(ord($char) > 127) //ord()函数取得第一个字符的ASCII码,如果大于0xa0(127)的话则是中文字符
    {
      $i+=2;//utf-8编码的情况下,一个中文字符占三个字节
      if($i < $len)
      {
        $arr[] = substr($str, 0, 3);//utf-8编码的情况下,一个中文字符占三个字节
        $str = substr($str, 3);
      }
    }
    else
    {
      $arr[] = $char;
      $str = substr($str, 1);//否则为非中文,占一个字符
    }
  }
  return join(array_reverse($arr));//以相反的元素顺序返回数组:
}
echo $str.&#39;+&#39;.cnstrrev($str);

3. Solution to json_encode UTF-8 Chinese garbled characters in php

This is a simple code conversion, and then returns the required array after urlcode

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,&#39;UTF-8&#39;,&#39;GBK&#39;));  
        }  
    }
  return $array;  
}

4. Collection of solutions to Chinese garbled characters transmitted in the php url address bar

url encoding
Syntax: string urlencode(string str);
Return value: String
Function type: Encoding processing

url decoding
Restore the URL encoded string.
Syntax: string urldecode(string str);
Return value: string
Function type: Encoding processing

5. json_encode in php handles gbk and gb2312 Chinese garbled problems Solution

It can be seen that even if UTF8-encoded characters are used, Chinese garbled characters appear when using json_encode. The solution is to process the characters with the function urlencode() before using json_encode, then json_encode, and then use the function urldecode() to convert them back when outputting the result.

6. Solution to the Ajax Chinese garbled problem in PHP

Because XMLHttp decodes the responseBody according to UTF-8 encoding when processing the returned responseText. If the server side If the data stream sent is indeed UTF-8 encoded, then Chinese characters will be displayed correctly, but if it is GBK or other encoding, the above situation will occur.

Related Questions and Answers

1. Encountered a magical problem caused by Chinese garbled characters

2. php Chinese garbled code

3. The Chinese garbled code on the webpage cannot be solved, see the picture

[Related recommendations]

1. PHP Chinese website free video tutorial: "php.cn Dugu Jiujian (4) - PHP video tutorial"

2. PHP Chinese website related articles: 《php introductory tutorial one week to learn PHP》

The above is the detailed content of Summary of solutions to the problem of php Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn