zoukankan      html  css  js  c++  java
  • 检测UTF-8编码

    在PHP检测字符串是否是UTF-8编码的时候,很多人在使用mb_detect_encoding的时候,经常遇到检测不准的问题,下面的方法可以准确检测编码是否是UTF-8

    function check_utf8($str)  
    {  
        $len = strlen($str);  
        for($i = 0; $i < $len; $i++){  
            $c = ord($str[$i]);  
            if ($c > 128) {  
                if (($c > 247)) return false;  
                elseif ($c > 239) $bytes = 4;  
                elseif ($c > 223) $bytes = 3;  
                elseif ($c > 191) $bytes = 2;  
                else return false;  
                if (($i + $bytes) > $len) return false;  
                while ($bytes > 1) {  
                    $i++;  
                    $b = ord($str[$i]);  
                    if ($b < 128 || $b > 191) return false;  
                    $bytes--;  
                }  
            }  
        }  
        return true;  
    } // end of check_utf8  
  • 相关阅读:
    if 语句
    变量命名
    python变量
    运算符
    注释
    PyCharm
    python版本,执行
    Tornado 【简述】
    Git 【管理篇】
    MySQL的介绍
  • 原文地址:https://www.cnblogs.com/whoamme/p/3465068.html
Copyright © 2011-2022 走看看