当前位置:Gxlcms > PHP教程 > PHP该怎么检测文件的编码呢

PHP该怎么检测文件的编码呢

时间:2021-07-01 10:21:17 帮助过:30人阅读

PHP 该如何检测文件的编码呢
用户上传一个文件,页面上显示出内容,整个网站都是UTF-8,用户如果上传了GB的文件,就显示出乱码。
可以用mb_convert_encoding($text, "UTF-8 ", "GBK ");来转换,但如何确定用户上传的是GB编码的文件呢?

------解决方案--------------------
mb_check_encoding -- Check if the string is valid for the specified encoding

------解决方案--------------------
貌似楼上写错了
mb_detect_encoding( $string )
------解决方案--------------------
iconv也可以。。。。
------解决方案--------------------
$temstr=file_get_contents($path);
$encode = mb_detect_encoding($temstr, "ASCII,UTF-8,CP936,EUC-CN,BIG-5,EUC-TW ");
$temstr=mb_convert_encoding($temstr, "CP936 ", $encode);

人气教程排行