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

Two methods for determining character encoding in PHP

WBOY
Release: 2016-07-25 09:00:19
Original
1115 people have browsed it
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);
?>
Copy after login

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;
}
?>
Copy after login

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



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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template