阮一峰的IT笔记:首页 -> 分类 -> PHP 查看所有文章:按分类 | 按月份

十步解决php utf-8编码(转贴)

php用UTF-8总结:

1、php文件本身必须是UTF-8编码。不像Java会生成class文件,避免这个问题

2、php要输出头:header(”Content-Type: text/html; charset=UTF-8″)

3、meta标签无所谓,有header所有浏览器就会按header来解析

4、所有外围都得用UTF8,包括数据库、*.js、*.css(CSS影响倒不大)

5、php本身不是Unicode的,所有substr之类的函数得改成mb_substr(需要装mbstring扩展);或者用iconv转码(基本上的linux都装了,没装的话download、tar、make、make install,很简单的)

6、my.ini:
[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8
default-storage-engine=MyISAM
在[mysqld]下加入:
default-collation=utf8_bin
init_connect='SET NAMES utf8'

7、在需要做数据库操作的php程序前面加上
mb_internal_encoding('utf-8');

8、create table最后边加上ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin

9、phpMyAdmin/config.inc.php
$cfg['DefaultCharset'] = 'utf-8';
$cfg['RecodingEngine'] = 'iconv';

10、phpAdmin导出数据时
把"二进制区域使用十六进制显示"的勾去掉

特别郁闷的:文件系统函数不支持UTF-8!

听说php6已经内置Unicode支持,以后试一下

« 在线编辑器实现原理(兼容IE和FireFox)(转) | 首页 | PHP的错误处理 »

About

This page contains a single entry from the blog posted on 2007年10月29日 13:22.

The previous post in this blog was 在线编辑器实现原理(兼容IE和FireFox)(转).

The next post in this blog is PHP的错误处理.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33

Post a comment