Home >Backend Development >PHP Tutorial >Two methods for determining character encoding in PHP

Two methods for determining character encoding in PHP

WBOY
WBOYOriginal
2016-07-25 09:00:191117browse
I would like to introduce two methods for PHP to determine the character encoding. One is to use the function mb_detect_encoding that comes with PHP, and the other is to write a function yourself to handle it. Friends in need, come and take a look.

Method 1, use mb_detect_encoding function.

<?php
$str=”程序员之家,欢迎大家的光临。”;
echo mb_detect_encoding($str);
?>

Method 2, custom function.

<?php
function chkbm($string){
    $bm = array(‘ASCII’, ‘GBK’, ‘UTF-8′);
    foreach($bm as $c){
        if( $string === iconv(‘UTF-8′, $c, iconv($c, ‘UTF-8′, $string))){//转换编码后是不是相等
            return $c;
        }
    }
    return null;
}
?>

How to choose? Smart one, you decide, ha~~~



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