快捷搜索:   nginx

解决乱码的常用手段

mysql_query("set names 'gbk'");

header('Content-Type:text/html;charset=gbk');

PHP网站全面采用UTF-8方法

1. 用 vi /etc/httpd/conf/httpd.conf 设定Apache中的语系为 (记得restart)
AddDefaultCharset UTF-8

2. 用 vi /etc/php.ini 设定php中的语系为 (记得restart)
default_charset = "utf-8"

3. 用 vi /etc/my.cnf 设定MySQL中的语系为 (记得restart)
[mysqld]
init_connect='SET NAMES utf8'
default-character-set=utf8
[client]
default-character-set = utf8

4. 建立资料库时选择语系: (记得清除DB Cache)
DROP DATABASE IF EXISTS `aa`;
CREATE DATABASE `aa` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE `aa`;

CREATE TABLE IF NOT EXISTS `aat` (
`id` char(1) NOT NULL default '1',
`myStr` varchar(200) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

5. 用UltraEdit(v11.20a版) 转换所有ANSI格式的php档案转化为UTF-8格式:
File --> Conversions --> ASCII to UTF-8 (Unicoding Editing)
( 在UltraEdit中按Advanced --> configuration --> File Handling
--> Unicode/UTF-8 Detection --> 剔选Auto detect utf-8 files )
如有需要时,可执行Remove BOM.php. 当用WinXP的Notepad将php档由ANSI转为UTF-8时,
因档头有BOM,会引起排版问题,故必须移除,执行Remove BOM.php即可自动移除.
Remove BOM.php可由以下网址下载:
http://www.hoyo.idv.tw/hoyoweb/document/view.php?sid=13&author=hoyo&status=view

6. 在php档中必须加入:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body>

7. 在连接DB的档中必须加入3行mysql_query才ok:
$host="localhost"; $DBname="aa";
$user= "root"; $passwd = "";
$link = mysql_connect($host,$user,$passwd) or die ("Fail");
$db = mysql_select_db($DBname, $link) or die ("Fail");
// 要在真正query DB取出资料前,加入以下3行
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER_SET_CLIENT=utf8");
mysql_query("SET CHARACTER_SET_RESULTS=utf8");
$sql = "select * from aat where crid='1'";
$rows = mysql_query($sql);

8. 在php档中, 如有需要须注意: [Optional]
运用htmlentities和htmlspecialchars时,要似如下:
$chars = htmlentities($chars,ENT_QUOTES,"UTF-8");
$chars = htmlspecialchars($chars,ENT_QUOTES,"UTF-8");
并且在显示前要用
$chars = html_entity_decode($chars,ENT_QUOTES,"UTF8");
如有用过addslashes()或mysql_real_escape_string()记得用以下:
$chars = stripslashes($chars);
如有需要可以用以下function将不同编码转换:
$chars = iconv('Big5','UTF-8',$chars); //由Big5转为UTF-8

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php header('Content-Type:text/html;charset=UTF-8'); ?>


顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论