PHP编码转换

来源:互联网 发布:微信淘宝联盟链接 编辑:程序博客网 时间:2024/06/14 10:57

要使用上面的函数需要安装但是需要先enable mbstring 扩展库。

 

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

PHP中的另外一个函数iconv也是用来转换字符串编码的,与上函数功能相似。 
下面还有一些详细的例子: 
iconv — Convert string to requested character encoding 
(PHP 4 >= 4.0.5, PHP 5) 
mb_convert_encoding — Convert character encoding 
(PHP 4 >= 4.0.6, PHP 5)

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

bjofp.codeplex.com;
beijfp.codeplex.com;
beijfpa.codeplex.com;
beijfps.codeplex.com;
hefp.codeplex.com;
heffp.codeplex.com;
hefpa.codeplex.com;
shuzfp.codeplex.com;
shuzfpa.codeplex.com;
shzfps.codeplex.com;
ninbfp.codeplex.com;
ninbfpa.codeplex.com;
nibfp.codeplex.com;
wuxfp.codeplex.com;
wuxfpa.codeplex.com;
wuxfps.codeplex.com;
nanjfp.codeplex.com;
nanjfpd.codeplex.com;
nanfp.codeplex.com;
csofp.codeplex.com;
changsfp.codeplex.com;
chsfp.codeplex.com;
cshfp.codeplex.com;
qingdfp.codeplex.com;
qdofp.codeplex.com;
cqfpq.codeplex.com;
cqafp.codeplex.com;
chqfp.codeplex.com;
cdafp.codeplex.com;
cdufp.codeplex.com;
cdufpa.codeplex.com;
shhfp.codeplex.com;
shqfp.codeplex.com;
fpsh.codeplex.com;
hbefp.codeplex.com;
bgbfp.codeplex.com;
haebfp.codeplex.com;
cchfp.codeplex.com;
cccyfp.codeplex.com;
ccdefp.codeplex.com;
syzsfp.codeplex.com;
sydefp.codeplex.com;
sycyfp.codeplex.com;
zzzsfp.codeplex.com;
zzcyfp.codeplex.com;
zzdefp.codeplex.com;
hzzsfp.codeplex.com;
hzcyfp.codeplex.com;
hzsefp.codeplex.com;
kmzsfp.codeplex.com;
kfp.codeplex.com;
kmfp1.codeplex.com;
dlfp1.codeplex.com;
dlcyfp.codeplex.com;
dalfp.codeplex.com;
wuhfp.codeplex.com;
wuhfp1.codeplex.com;
wuhfps.codeplex.com;
fuzfp.codeplex.com;
fuzfp1.codeplex.com;
fzfp1.codeplex.com;
gzfp1.codeplex.com;
guzfp.codeplex.com;
guzfp1.codeplex.com;
sjzp.codeplex.com;
sjzfp1.codeplex.com;
sjzfp2.codeplex.com;
xafp1.codeplex.com;
xafp2.codeplex.com;
xafp3.codeplex.com;
shenzfp.codeplex.com;
szfp1.codeplex.com;
szfp2.codeplex.com;
tyfp1.codeplex.com;
tyfp2.codeplex.com;
tyfp3.codeplex.com;
nczsfp.codeplex.com;
nccyfp.codeplex.com;
ncdefp.codeplex.com;
gyzsfp.codeplex.com;
gycyfp.codeplex.com;
gydefp.codeplex.com;
dgfp1.codeplex.com;
dgfp2.codeplex.com;
iconv使用: 
发现iconv在转换字符”—”(ascii码150之后的字符)到gb2312时会出错,如果没有ignore参数,

所有该字符后面的字符串都无法被保存。不管怎么样,这个”—”都无法转换成功,无法输出。 另外mb_convert_encoding没有这个bug. 
一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数. 
from_encoding is specified by character code name before conversion.

it can be array or string - comma separated enumerated list. If it is not specified, the internal encoding will be used. 
/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */ 
$str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”); 
/* “auto” is expanded to “ASCII,JIS,UTF-8,EUC-JP,SJIS” */ 
$str = mb_convert_encoding($str, “EUC-JP”, “auto”);

iconv例子: 
以下为引用的内容: 

Java代码  收藏代码
  1. $content = iconv(”GBK”, “UTF-8″, $content);   
  2. $content = mb_convert_encoding($content, “UTF-8″, “GBK”);   

 
 

PHP中使用mb_convert_encoding转码的小陷阱
php程序中使用mb_convert_encoding()方法进行字符编码转换大家都很熟悉了,平时也在大量的使用。

而且在一般情况下该方法也表现的足够好,值得表扬。但在一个项目中我们需要使用它进行UTF8到GBK的转换,

在转换一些特殊字符时发现了一个不大不小的问题。

具体表现为mb把在utf8可编码的字符而在gbk中不可编码的字符都转成了

0 0
原创粉丝点击