Home php教程 php手册 PHP乱码问题,UTF

PHP乱码问题,UTF

Jun 06, 2016 pm 07:58 PM
php utf-8 Garbled characters question page

一.HTML页面转UTF-8编码问题 1.在head后,title前加入一行: meta http-equiv='Content-Type' content='text/html; charset=utf-8' / 顺序不能错,一定要在 显示的标题有可能是乱码! 2.html文件编码问题: 点击编辑器的菜单:“文件”-“另存为”,可以看到

一.HTML页面转UTF-8编码问题 
1.在head后,title前加入一行:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
Copy after login


顺序不能错,一定要在

显示的标题有可能是乱码!

2.html文件编码问题:

点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8, 
如果是ANSI,需要将编码改成:UTF-8。 
3.HTML文件头BOM问题: 




2.可以用EditPlus打开文件,并在菜单“首选项”->“文件”->"UTF-8标识",设置为:“总是删除签名”, 

4.WEB服务器UTF-8编码问题: 
如果你按以上所列的步骤做了,还是有中文乱码问题, 
请检查你的所使用的WEB服务器的编码问题 
如果你使用的是Apache,请将配置文件里的:charset 设成:utf-8(这里仅列出方法,具体格式请参考apache的配置文件) 
如果你使用的是Nginx,请将nginx.conf里的:charset 设成 utf-8, 
具体找到 "charset gb2312;"或者类似的语句,改成:“charset utf-8;”。

二.PHP页面转UTF-8编码问题 
1.在代码开始出加入一行: 

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


2.PHP文件编码问题

点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8, 
如果是ANSI,需要将编码改成:UTF-8。 
3.PHP文件头BOM问题: 

否则,会出现session不能使用的情况,并有类似的提示: 
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent 





2.可以用EditPlus打开文件,并在菜单“首选项”->“文件”->"UTF-8标识",设置为:“总是删除签名”, 

4.PHP以附件形式保存文件的时候,UTF-8编码问题: 
PHP以附件形式保存文件,文件名必须是GB2312编码, 
否则,如果文件名中有中文的话,将是显示乱码: 
如果你的PHP本身是UTF-8编码格式的文件, 
需要将文件名变量由UTF-8转成GB2312: 
iconv("UTF-8", "GB2312", "$filename"); 

5.截断显示文章标题时,出现乱码或者“?”问号的问题: 
一般文章标题很长的时候,会显示一部分标题,会对文章标题进行截断, 
由于一个UTF-8编码格式的中文字符会占用3个字符宽度, 
截取标题的时候,有时会只截取到一个中文字符的1个字符或2字符宽度, 
没截取完整,将出现乱码或“?”问号的情况, 
用下面的函数截取标题,就不会有问题:

function get_brief_str($str, $max_length)
{
echo strlen($str) ."<br>";
if(strlen($str) > $max_length)
{
$check_num = 0;
for($i=0; $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;
} 
Copy after login


三.MYSQL数据库使用UTF-8编码的问题

 

1.用phpmyadmin创建数据库和数据表 
创建数据库的时候,请将“整理”设置为:“utf8_general_ci” 
或执行语句:

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 
Copy after login


创建数据表的时候:如果是该字段是存放中文的话,则需要将“整理”设置为:“utf8_general_ci”,

如果该字段是存放英文或数字的话,默认就可以了。

相应的SQL语句,例如:

CREATE TABLE `test` (
`id` INT NOT NULL ,
`name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ; 
Copy after login


2.用PHP读写数据库

在连接数据库之后:

[hide]$connection = mysql_connect($host_name, $host_user, $host_pass);

加入两行:

mysql_query("set character set 'utf8'");//读库

mysql_query("set names 'utf8'");//写库
Copy after login


就可以正常的读写MYSQL数据库了。

 

 

四.JS相关的UTF-8编码问题 
1.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]) <br>
<br>

<p><span>JS读cookie的时候,用unescape解码,</span></p>
<p><span>然后就解决cookie中有中文乱码的问题了。</span></p>
<p><span>2.外部JS文件UTF-8编码问题</span></p>
<p><span>当一个HTML页面或则PHP页面包含一个外部的JS文件时,</span></p>
<p><span>如果HTML页面或则PHP页面是UTF-8编码格式的文件,</span></p>
<p><span>外部的JS文件同样要转成UTF-8的文件,</span></p>
<p><span>否则将出现,没有包含不成功,调用函数时没有反应的情况。</span></p>
<p><span>点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,</span></p>
<p><span>如果是ANSI,需要将编码改成:UTF-8。</span></p>
<p><strong><span>五.FLASH相关的UTF-8编码问题</span></strong></p>
<p><span>FLASH内部对所有字符串,默认都是以UTF-8处理 <br>
1.FLASH读文普通本文件(txt,html) <br>
要将文本文件的编码存为UTF-8 <br>
点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8, <br>
如果是ANSI,需要将编码改成:UTF-8。 <br>
2.FLASH读XML文件 <br>
要将XML文件的编码存为UTF-8 <br>
点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8, <br>
如果是ANSI,需要将编码改成:UTF-8。 <br>
在XML第1行写: <br>
<br>
3.FLASH读PHP返回数据 <br>
如果PHP编码本身是UTF-8的,直接echo就可以了 <br>
如果PHP编码本身是GB2312的,可以将PHP转存成UTF-8编码格式的文件,直接echo就可以了<br>
如果PHP编码本身是GB2312的,而且不允许改文件的编码格式, <br>
用下面的语句将字符串转换成UTF-8的编码格式 <br>
$new_str = iconv("GB2312", "UTF-8", "$str"); <br>
再echo就可以了 <br>
4.FLASH读数据库(MYSQL)的数据 <br>
FLASH要通过PHP读取数据库中的数据 <br>
PHP本身的编码不重要,关键是如果数据库的编码是GB2312的话, <br>
需要用下面的语句将字符串转换成UTF-8的编码格式 <br>
$new_str = iconv("GB2312", "UTF-8", "$str"); <br>
<br>
5.FLASH通过PHP写数据 <br>
一句话,FLASH传过来的字符串是UTF-8格式的, <br>
要转换成相应的编码格式,再操作(写文件、写数据库、直接显示等等) <br>
还是用iconv函数转换 <br>
6.FLASH使用本地编码(理论上不推荐使用) <br>
如果想让FLASH不使用UTF-8编码,而是使用本地编码 <br>
对于中国大陆地区而言,本地编码是GB2312或GBK <br>
AS程序内,可以添加以下代码: <br>
System.useCodepage = true; <br>
那么FLASH内所有字符都是使用GB2312的编码了 <br>
所有导入到FLASH或者从FLASH导出的数据,都应该做相应的编码转换 <br>
因为使用本地编码,会造成使用繁体中文地区的用户产生乱码,所以不推荐使用</span></p>


Copy after login
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles