About the problem of garbled characters in PHP
This article mainly introduces the problem of garbled characters in PHP. Friends who are interested can learn about it. I hope it will be helpful to everyone. 1. HTML page conversion to UTF-8 encoding problem
1. Add a line after the head and before the title:
The order cannot be wrong, the title displayed in
may be garbled!
2.html file encoding problem:
Click the menu of the editor: "File"->"Save As", you can see the encoding of the current file, make sure the file encoding is: UTF -8,
If it is ANSI, the encoding needs to be changed to: UTF-8.
3.HTML file header BOM problem:
When converting a file from other encoding to UTF-8 encoding, sometimes a BOM tag will be added at the beginning of the file.
The BOM tag may This will cause the browser to display garbled characters when displaying Chinese characters.
Methods to delete this BOM tag:
1. You can open the file with Dreamweaver and resave it to remove the BOM tag!
2. You can open the file with EditPlus, and in the menu "Preferences"->"File"->"UTF-8 Identity", set it to: "Always delete signatures",
Then save the file , that is, the BOM label can be removed!
4. WEB server UTF-8 encoding problem:
If you follow the steps listed above and still have Chinese garbled problems,
Please check the encoding problem of the WEB server you are using
If you are using Apache, please set the charset in the configuration file to: utf-8 (only the methods are listed here, please refer to the apache configuration file for the specific format)
If you are using Nginx, please set In nginx.conf: charset is set to utf-8,
Specifically find "charset gb2312;" or a similar statement, and change it to: "charset utf-8;".
2. PHP page conversion to UTF-8 encoding problem
1. Add a line at the beginning of the code:
header("Content-Type: text/html;charset= utf-8");
2.PHP file encoding problem
Click the editor's menu: "File"->"Save As", you can see the encoding of the current file, make sure The file encoding is: UTF-8,
If it is ANSI, the encoding needs to be changed to: UTF-8.
3.PHP file header BOM problem:
PHP files must not have BOM tags
Otherwise, the session will not be usable, and there will be similar prompts:
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 regarded this BOM tag as output, so an error occurred!
So the PHP page must delete the BOM tag
Methods to delete this BOM tag:
1. You can open the file with Dreamweaver and resave it to remove the BOM tag!
2. You can open the file with EditPlus, and in the menu "Preferences"->"File"->"UTF-8 Identity", set it to: "Always delete signatures",
Then save the file , that is, the BOM label can be removed!
4. UTF-8 encoding problem when PHP saves files as attachments:
When PHP saves files as attachments, the file name must be GB2312 encoded,
Otherwise, if there is Chinese in the file name, It will display 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");
5. When the article title is truncated and displayed, garbled characters or "?" question marks appear:
Generally, when the article title is very long, part of the title will be displayed. Truncate the title of the article,
Since a Chinese character in UTF-8 encoding format will occupy 3 characters of width,
When intercepting the title, sometimes only 1 character or 2 characters of width of a Chinese character will be intercepted ,
If the interception is not complete, garbled characters or "?" question marks will appear.
Use the following function to intercept the title, and there will be no problem:
function get_brief_str($str, $max_length) { echo strlen($str) ."<br>"; if(strlen($str) > $max_length) { $check_num = 0; for($i=0; $i < $max_length; $i++) { if (ord($str[$i]) > 128) $check_num++; } if($check_num % 3 == 0) $str = substr($str, 0, $max_length)."..."; else if($check_num % 3 == 1) $str = substr($str, 0, $max_length + 2)."..."; else if($check_num % 3 == 2) $str = substr($str, 0, $max_length + 1)."..."; } return $str; }
3. MYSQL database usage Problems with UTF-8 encoding
1. Use phpmyadmin to create databases and data tables
When creating a database, please set "Organization" to: "utf8_general_ci"
or Execution statement:
CREATE DATABASE `dbname` DEFAULTCHARACTER SET utf8 COLLATE utf8_general_ci;
When creating a data table: If the field stores Chinese, you need to set "Collation" to: "utf8_general_ci",
If the field stores English or numbers, the default is fine.
Corresponding SQL statement, for example:
CREATE TABLE `test` ( `id` INT NOT NULL , `name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ciNOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM ;
2. Use PHP to read and write the database
After connecting to the database:
[hide]$connection = mysql_connect($host_name, $host_user, $host_pass);
Add two lines:
mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库
You can read and write the MYSQL database normally.
4. JS related UTF-8 encoding issues
JS读Cookie的中文乱码问题
PHP写cookie的时候需要将中文字符进行escape编码,
否则JS读到cookie中的中文字符将是乱码。
但php本身没有escape函数,我们新写一个escape函数:
function escape($str) { preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r); $ar = $r[0]; foreach($ar as $k=>$v) { if(ord($v[0]) < 128) $ar[$k] = rawurlencode($v); else $ar[$k] = "%u".bin2hex(iconv("UTF-8","UCS-2",$v)); } return join("",$ar); }
JS读cookie的时候,用unescape解码,
然后就解决cookie中有中文乱码的问题了。
2.外部JS文件UTF-8编码问题
当一个HTML页面或则PHP页面包含一个外部的JS文件时,
如果HTML页面或则PHP页面是UTF-8编码格式的文件,
外部的JS文件同样要转成UTF-8的文件,
否则将出现,没有包含不成功,调用函数时没有反应的情况。
点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,
如果是ANSI,需要将编码改成:UTF-8。
五.FLASH相关的UTF-8编码问题
FLASH内部对所有字符串,默认都是以UTF-8处理
1.FLASH读文普通本文件(txt,html)
要将文本文件的编码存为UTF-8
点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,
如果是ANSI,需要将编码改成:UTF-8。
2.FLASH读XML文件
要将XML文件的编码存为UTF-8
点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,
如果是ANSI,需要将编码改成:UTF-8。
在XML第1行写:
3.FLASH读PHP返回数据
如果PHP编码本身是UTF-8的,直接echo就可以了
如果PHP编码本身是GB2312的,可以将PHP转存成UTF-8编码格式的文件,直接echo就可以了
如果PHP编码本身是GB2312的,而且不允许改文件的编码格式,
用下面的语句将字符串转换成UTF-8的编码格式
$new_str = iconv("GB2312", "UTF-8", "$str");
再echo就可以了
4.FLASH读数据库(MYSQL)的数据
FLASH要通过PHP读取数据库中的数据
PHP本身的编码不重要,关键是如果数据库的编码是GB2312的话,
需要用下面的语句将字符串转换成UTF-8的编码格式
$new_str = iconv("GB2312", "UTF-8", "$str");
5.FLASH通过PHP写数据
一句话,FLASH传过来的字符串是UTF-8格式的,
要转换成相应的编码格式,再操作(写文件、写数据库、直接显示等等)
还是用iconv函数转换
6.FLASH使用本地编码(理论上不推荐使用)
如果想让FLASH不使用UTF-8编码,而是使用本地编码
对于中国大陆地区而言,本地编码是GB2312或GBK
AS程序内,可以添加以下代码:
System.useCodepage = true;
那么FLASH内所有字符都是使用GB2312的编码了
所有导入到FLASH或者从FLASH导出的数据,都应该做相应的编码转换
因为使用本地编码,会造成使用繁体中文地区的用户产生乱码,所以不推荐使用
相关推荐:
The above is the detailed content of About the problem of garbled characters in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
