Rumah > php教程 > php手册 > teks badan

php检测文件编码方法,

WBOY
Lepaskan: 2016-06-16 09:16:25
asal
822 orang telah melayarinya

php检测文件编码方法,

关于文件编码的检测,百度一下一大把都是,但是确实没有能用的、
很多人建议 mb_detect_encoding 检测,可是不知为何我这不成功,什么都没输出、
看到有人写了个增强版,用 BOM 判断的,我果断就无视了,这东西完全不靠谱、
最终根据PHP手册里 mb_detect_encoding 函数下方的例子,自己写了一个检测函数,
还包括自动检测编码并按指点编码读取文件的函数、
源码献上,不喜勿喷。
网上的方法我试过没用才写的,说不定环境不一样导致的。
所以万一没用,也别喷我,我只是共享想思路而已、、

文本
<span class="hljs-preprocessor"><?php
<span class="hljs-comment">/**
 * 检测文件编码
 *<span class="hljs-phpdoc"> @param string $file 文件路径
 *<span class="hljs-phpdoc"> @return string|null 返回 编码名 或 null
 */
<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">detect_encoding<span class="hljs-params">(<span class="hljs-variable">$file) {
    <span class="hljs-variable">$list = <span class="hljs-keyword">array(<span class="hljs-string">'GBK', <span class="hljs-string">'UTF-8', <span class="hljs-string">'UTF-16LE', <span class="hljs-string">'UTF-16BE', <span class="hljs-string">'ISO-8859-1');
    <span class="hljs-variable">$str = file_get_contents(<span class="hljs-variable">$file);
    <span class="hljs-keyword">foreach (<span class="hljs-variable">$list <span class="hljs-keyword">as <span class="hljs-variable">$item) {
        <span class="hljs-variable">$tmp = mb_convert_encoding(<span class="hljs-variable">$str, <span class="hljs-variable">$item, <span class="hljs-variable">$item);
        <span class="hljs-keyword">if (md5(<span class="hljs-variable">$tmp) == md5(<span class="hljs-variable">$str)) {
            <span class="hljs-keyword">return <span class="hljs-variable">$item;
        }
    }
    <span class="hljs-keyword">return <span class="hljs-keyword">null;
}

<span class="hljs-comment">/**
 * 自动解析编码读入文件
 *<span class="hljs-phpdoc"> @param string $file 文件路径
 *<span class="hljs-phpdoc"> @param string $charset 读取编码
 *<span class="hljs-phpdoc"> @return string 返回读取内容
 */
<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">auto_read<span class="hljs-params">(<span class="hljs-variable">$file, <span class="hljs-variable">$charset=<span class="hljs-string">'UTF-8') {
    <span class="hljs-variable">$list = <span class="hljs-keyword">array(<span class="hljs-string">'GBK', <span class="hljs-string">'UTF-8', <span class="hljs-string">'UTF-16LE', <span class="hljs-string">'UTF-16BE', <span class="hljs-string">'ISO-8859-1');
    <span class="hljs-variable">$str = file_get_contents(<span class="hljs-variable">$file);
    <span class="hljs-keyword">foreach (<span class="hljs-variable">$list <span class="hljs-keyword">as <span class="hljs-variable">$item) {
        <span class="hljs-variable">$tmp = mb_convert_encoding(<span class="hljs-variable">$str, <span class="hljs-variable">$item, <span class="hljs-variable">$item);
        <span class="hljs-keyword">if (md5(<span class="hljs-variable">$tmp) == md5(<span class="hljs-variable">$str)) {
            <span class="hljs-keyword">return mb_convert_encoding(<span class="hljs-variable">$str, <span class="hljs-variable">$charset, <span class="hljs-variable">$item);
        }
    }
    <span class="hljs-keyword">return <span class="hljs-string">"";
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Salin selepas log masuk

 

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan