Many people suggested mb_detect_encoding detection, but for some reason it failed and nothing was output.
I saw someone wrote an enhanced version that was judged by BOM, so I ignored it. This thing is totally unreliable.
Finally, based on the example below the mb_detect_encoding function in the PHP manual, I wrote a detection function myself,
It also includes functions that automatically detect encoding and read files according to the pointed encoding,
The source code is provided, if you don't like it, don't spray it.
I wrote this after trying the online methods but they didn’t work. Maybe it’s because of the different environment.
So if it doesn’t work, don’t criticize me, I’m just sharing my thoughts,
<span 1</span> <?<span php
</span><span 2</span> <span /*</span><span *
</span><span 3</span> <span * 检测文件编码
</span><span 4</span> <span * @param string $file 文件路径
</span><span 5</span> <span * @return string|null 返回 编码名 或 null
</span><span 6</span> <span */</span>
<span 7</span> <span function</span> detect_encoding(<span $file</span><span ) {
</span><span 8</span> <span $list</span> = <span array</span>('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1'<span );
</span><span 9</span> <span $str</span> = <span file_get_contents</span>(<span $file</span><span );
</span><span 10</span> <span foreach</span> (<span $list</span> <span as</span> <span $item</span><span ) {
</span><span 11</span> <span $tmp</span> = mb_convert_encoding(<span $str</span>, <span $item</span>, <span $item</span><span );
</span><span 12</span> <span if</span> (<span md5</span>(<span $tmp</span>) == <span md5</span>(<span $str</span><span )) {
</span><span 13</span> <span return</span> <span $item</span><span ;
</span><span 14</span> <span }
</span><span 15</span> <span }
</span><span 16</span> <span return</span> <span null</span><span ;
</span><span 17</span> <span }
</span><span 18</span>
<span 19</span> <span /*</span><span *
</span><span 20</span> <span * 自动解析编码读入文件
</span><span 21</span> <span * @param string $file 文件路径
</span><span 22</span> <span * @param string $charset 读取编码
</span><span 23</span> <span * @return string 返回读取内容
</span><span 24</span> <span */</span>
<span 25</span> <span function</span> auto_read(<span $file</span>, <span $charset</span>='UTF-8'<span ) {
</span><span 26</span> <span $list</span> = <span array</span>('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1'<span );
</span><span 27</span> <span $str</span> = <span file_get_contents</span>(<span $file</span><span );
</span><span 28</span> <span foreach</span> (<span $list</span> <span as</span> <span $item</span><span ) {
</span><span 29</span> <span $tmp</span> = mb_convert_encoding(<span $str</span>, <span $item</span>, <span $item</span><span );
</span><span 30</span> <span if</span> (<span md5</span>(<span $tmp</span>) == <span md5</span>(<span $str</span><span )) {
</span><span 31</span> <span return</span> mb_convert_encoding(<span $str</span>, <span $charset</span>, <span $item</span><span );
</span><span 32</span> <span }
</span><span 33</span> <span }
</span><span 34</span> <span return</span> ""<span ;
</span><span 35</span> }
Copy after login
http://www.bkjia.com/PHPjc/699432.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/699432.htmlTechArticleMany people suggested mb_detect_encoding detection, but for some reason it failed and nothing was output. I saw someone wrote An enhanced version, judged by BOM, I decisively ignored it, this...