Maison >développement back-end >tutoriel php >php检测文件编码方法[非完美]_PHP教程
<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> }