Home > php教程 > PHP源码 > body text

php mb_detect_encoding判断字符串编码方法

WBOY
Release: 2016-06-08 17:22:02
Original
1076 people have browsed it

在php中利用mb_detect_encoding可以判断给我们的字符是那个类型的编码了,从而我们就可以利用inconv进行字符转换了,下面给大家介绍几个例子。

<script>ec(2);</script>

例子,利用mb_detect_encoding()判断字符是否为uft-8编码。

 代码如下 复制代码

$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 存在一个硬伤,经常出现判断不准确的情况。或许这样就可以解决:

 代码如下 复制代码

// 使用 iconv 转换并判断是否等值,效率不高
function is_utf8 ($str) {
    if ($str === iconv('UTF-8', 'UTF-8//IGNORE', $str)) {
        return 'UTF-8';
    }
}
// 多种编码的情况
function detect_encoding ($str) {
    foreach (array('GBK', 'UTF-8') as $v) {
        if ($str === iconv($v, $v . '//IGNORE', $str)) {
            return $v;
        }
    }
}

通过以上方式得到字符串编码信息后,就可以利用 iconv 或 mb_convert_encoding 来转换编码了

问题


可是当 $keytitle  = '%D0%BE%C6%AC'; 时。

检测结果却是UTF-8,这其实不算是bug,我们不应当过于依赖mb_detect_encoding,当字符串较短时,检测结果产生偏差的可能性很大。

解决方法:
 

 代码如下 复制代码

$encode = mb_detect_encoding($keytitle, array('ASCII','GB2312′,'GBK’,'UTF-8');

 x

 

Related labels:
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 Recommendations
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!