define ('utf32_big_endian_bom' , chr(0x00) . chr(0x00) . chr(0xfe) . chr(0xff));
define ('utf32_little_endian_bom', chr(0xff) . chr(0xfe) . chr(0x00) . chr(0x00));
define ('utf16_big_endian_bom' , chr(0xfe) . chr(0xff));
define ('utf16_little_endian_bom', chr(0xff) . chr(0xfe));
define ('utf8_bom' , chr(0xef) . chr(0xbb) . chr(0xbf));
function detect_utf_encoding($text) {
$first2 = substr($text, 0, 2);
$first3 = substr($text, 0, 3);
$first4 = substr($text, 0, 3);
if ($first3 == utf8_bom) return 'utf-8';
elseif ($first4 == utf32_big_endian_bom) return 'utf-32be';
elseif ($first4 == utf32_little_endian_bom) return 'utf-32le';
elseif ($first2 == utf16_big_endian_bom) return 'utf-16be';
elseif ($first2 == utf16_little_endian_bom) return 'utf-16le';
}
function getfileencoding($str){
$encoding=mb_detect_encoding($str);
if(empty($encoding)){
$encoding=detect_utf_encoding($str);
}
return $encoding;
}
$file = 'text1.txt';
echo getfileencoding(file_get_contents($file)); // 输出ascii
echo '';
$file = 'text2.txt';
echo getfileencoding(file_get_contents($file)); // 输出utf-8
echo '';
$file = 'text3.txt';
echo getfileencoding(file_get_contents($file)); // 输出utf-16le
echo '';
