Home >php教程 >php手册 >检测字符串是否为UTF8编码

检测字符串是否为UTF8编码

WBOY
WBOYOriginal
2016-06-07 11:45:481319browse

/**<br>  * 检测字符串是否为UTF8编码<br>  * @param  string  $str 被检测的字符串<br>  * @return boolean      <br>  */<br> function is_utf8($str){<br>     $len = strlen($str); <br>     for($i = 0; $i          $c = ord($str[$i]); <br>         if ($c > 128) { <br>             if (($c > 247)) return false; <br>             elseif ($c > 239) $bytes = 4; <br>             elseif ($c > 223) $bytes = 3; <br>             elseif ($c > 191) $bytes = 2; <br>             else return false; <br>             if (($i + $bytes) > $len) return false; <br>             while ($bytes > 1) { <br>                 $i++; <br>                 $b = ord($str[$i]); <br>                 if ($b  191) return false; <br>                 $bytes--; <br>             } <br>         } <br>     } <br>     return true;<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
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