php字符编码转换问题 mb_convert_encoding与iconv函数

WBOY
Release: 2016-07-25 08:53:44
Original
972 people have browsed it
  1. header("content-type: text/html; charset=utf-8");
  2. echo mb_convert_encoding("妳係我的友仔", "utf-8", "gbk");
  3. ?>
复制代码

gb2312 to big5编码转换:

  1. header("content-type: text/html; charset=big5");
  2. echo mb_convert_encoding("你是我的朋友", "big5", "gb2312");
  3. ?>
复制代码

如果使用以上函数,需要安装但是需要先enable mbstring 扩展库。

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )需要先enable mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别,但是执行效率比iconv差太多;

string iconv ( string in_charset, string out_charset, string str ) 注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://translit 和 //ignore,其中 //translit 会自动将不能直接转化的字符变成一个或多个近似的字符,//ignore 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。

一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数。

  1. $content = iconv("gbk", "utf-8″, $content);
  2. $content = mb_convert_encoding($content, "utf-8″, "gbk");
复制代码


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!