php 判断网页是否是utf8编码的方法
程序员文章站
2023-12-13 15:15:46
//判断编码复制代码 代码如下:$encode = mb_detect_encoding($q, array('gb2312','gbk','utf-8'));echo $...
//判断编码
$encode = mb_detect_encoding($q, array('gb2312','gbk','utf-8'));
echo $encode."<br/>";
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."<br/>";
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编码,防止出现乱码等情况。