PHP detection file Encoding method,
About the detection of file encoding, Baidu a lot of it is, but there is no use,
Many people recommend mb_detect_encoding detection, but I do not know why this is not successful, nothing output,
See someone wrote an enhanced version, with the BOM to judge, I decisively ignored, this thing completely not reliable,
Finally, based on the example below the Mb_detect_encoding function in the PHP manual, I wrote a detection function,
It also includes functions that automatically detect the encoding and read the file by pointing code,
Source, do not like to spray.
Online method I tried not to write, perhaps the environment is not the same result.
So if it doesn't work, don't squirt me, I'm just sharing thinking,
Text
/** * Detection File Encoding *@param string $file file path *@return String|null return the encoded name or NULL */functionDetect_encoding($file) {$list =Array' GBK ',' UTF-8 ',' Utf-16le ',' Utf-16be ',' Iso-8859-1 ');$str = file_get_contents ($file);foreach ($listAs$item) {$tmp = mb_convert_encoding ($STR,$item,$item);if (MD5 ($TMP) = = MD5 ($STR)) {Return$item; } }Returnnull;}/** * Automatically parse code read in file *@param string $file file path *@param string $charset Read encoding *@return string to return read */functionAuto_read($file,$charset =' UTF-8 ') {$list =Array' GBK ',' UTF-8 ', ' utf-16be ', $str = file_get_contents ( $file); foreach ( $list as $item) { $tmp = mb_convert_encoding ( $str, Span class= "hljs-variable" > $item, $item); if (MD5 ( $tmp) = = MD5 ( $str)) { Span class= "Hljs-keyword" >return mb_convert_encoding ( $str, $charset, $item);} } return ";}
http://www.bkjia.com/PHPjc/1087779.html www.bkjia.com true http://www.bkjia.com/PHPjc/1087779.html techarticle PHP Detection File encoding method, about the detection of file encoding, Baidu A lot of is, but really did not use, many people suggest mb_detect_encoding detection, but I do not know why ...