Home  >  Article  >  Backend Development  >  What should I do if the UFT-8 Chinese encoding in PHP is garbled?

What should I do if the UFT-8 Chinese encoding in PHP is garbled?

coldplay.xixi
coldplay.xixiOriginal
2020-07-21 11:12:552668browse

The solution to the garbled Chinese encoding of uft-8 in php: first add a line of relevant code at the beginning of the code; then click [File-Save As] in order to ensure that the file encoding is [UTF-8]; finally php Just delete the BOM tag from the page.

What should I do if the UFT-8 Chinese encoding in PHP is garbled?

Solution to garbled Chinese encoding of uft-8 in php:

1. At the beginning of the code Add a line at:

header("Content-Type: text/html;charset=utf-8");

2.PHP file encoding problem
Click the editor menu: "File"->"Save As" to see the current For the encoding of the file, make sure the file encoding is: UTF-8. If
is ANSI, you need to change the encoding to: UTF-8.

3. PHP file header BOM problem:
PHP files must not have BOM tags, otherwise, the session will not be used, and there will be Similar tips:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

This is because when executing session_start(), the entire page cannot have output, but when the BOM tag exists on the previous PHP page, PHP This BOM tag was treated as output, so an error occurred! Therefore, the BOM tag must be deleted from the PHP page.
How to delete this BOM tag:
1. You can open the file with Dreamweaver and resave it, that is, you can remove the BOM tag

2. You can open the file with EditPlus, And in the menu "Preferences"->"File"->"UTF-8 Identification", set it to: "Always remove signatures",
Then save the file, you can remove the BOM tag!

4. When PHP saves files as attachments, UTF-8 encoding issues: PHP saves files as attachments, and the file name must be GB2312 encoded. Otherwise, if there is Chinese in the file name, it will be displayed Garbled characters.

If your PHP itself is a file in UTF-8 encoding format, you need to convert the file name variable from UTF-8 to GB2312:

iconv("UTF-8", "GB2312", "$filename");

Use Program to illustrate the character interception method

function utf8_substr($str,$len) 
{ 
  for($i=0;$i<$len;$i++) 
  { 
    $temp_str=substr($str,0,1); 
    if(ord($temp_str) > 127){ 
      $i++; 
    if($i<$len){ 
      $new_str[]=substr($str,0,3); 
      $str=substr($str,3); 
      } 
    }else { 
    $new_str[]=substr($str,0,1); 
    $str=substr($str,1); 
    } 
  } 
  return join($new_str); 
}

Related learning recommendations:PHP programming from entry to proficiency

The above is the detailed content of What should I do if the UFT-8 Chinese encoding in PHP is garbled?. 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