您现在的位置是:首页 > 后端 > PHP网站首页PHP

php检测文件编码

简介php检测文件编码: /** * 检测文件编码 * @param string $file 文件路径 * @return string|null 返回 编码名 或 null

php检测文件编码:

/**
* 检测文件编码
* @param string $file 文件路径
* @return string|null 返回 编码名 或 null
*/
 function detect_encoding($file) {
     $list = array('GBK', 'UTF-8', 'UTF-16LE');
     $str = file_get_contents($file);
     foreach ($list as $item) {
         $tmp = mb_convert_encoding($str, $item, $item);
         if (md5($tmp) == md5($str)) {
             return $item;
         }
     }
     return null;
}


文章评论

Top