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前加入一行:

1

<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.在代码开始出加入一行: 

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字符宽度, 
没截取完整,将出现乱码或“?”问号的情况, 
用下面的函数截取标题,就不会有问题:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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” 
或执行语句:

1

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Copy after login


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

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

相应的SQL语句,例如:

1

2

3

4

5

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);

加入两行:

1

2

3

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函数: 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles