当前位置: 编程技术>php
php 判断网页是否是utf8编码的方法
来源: 互联网 发布时间:2014-08-26
本文导语: //判断编码 代码如下:$encode = mb_detect_encoding($q, array('GB2312','GBK','UTF-8'));echo $encode."";if($encode=="GB2312"){ $q = iconv("GBK","UTF-8",$q);}else if($encode=="GBK"){ $q = iconv("GBK","UTF-8",$q);}else if($encode=="EUC-CN"){ $q = iconv("GBK","UTF-8",$q);}else/...
//判断编码
$encode = mb_detect_encoding($q, array('GB2312','GBK','UTF-8'));
echo $encode."
";
if($encode=="GB2312")
{
$q = iconv("GBK","UTF-8",$q);
}
else if($encode=="GBK")
{
$q = iconv("GBK","UTF-8",$q);
}
else if($encode=="EUC-CN")
{
$q = iconv("GBK","UTF-8",$q);
}
else//CP936
{
//$q = iconv("GB2312","UTF-8",$q);
}
其实就是利用了mb_detect_encoding函数,如果不是utf8编码就转换为utf8编码,防止出现乱码等情况。
代码如下:
$encode = mb_detect_encoding($q, array('GB2312','GBK','UTF-8'));
echo $encode."
";
if($encode=="GB2312")
{
$q = iconv("GBK","UTF-8",$q);
}
else if($encode=="GBK")
{
$q = iconv("GBK","UTF-8",$q);
}
else if($encode=="EUC-CN")
{
$q = iconv("GBK","UTF-8",$q);
}
else//CP936
{
//$q = iconv("GB2312","UTF-8",$q);
}
其实就是利用了mb_detect_encoding函数,如果不是utf8编码就转换为utf8编码,防止出现乱码等情况。