Home >Backend Development >PHP Tutorial >PHP detection file encoding method, _PHP tutorial
Regarding file encoding detection, Baidu has a lot of them, but there is really no useful one.
Many people suggest mb_detect_encoding detection , but for some reason I didn’t succeed, and nothing was output.
I saw someone wrote an enhanced version, which was judged by BOM, so I ignored it decisively. This thing is completely unreliable.
Finally, according to PHP In the example below the mb_detect_encoding function in the manual, I wrote a detection function myself.
It also includes a function that automatically detects the encoding and reads the file according to the pointed encoding.
The source code is provided. If you don’t like it, don’t comment.
I wrote this after trying the online method but it didn’t work. Maybe it’s caused by the different environment.
So if it doesn’t work, don’t criticize me, I’m just sharing my thoughts,,
<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>