エンコード判別mb_detect_encodingの性能を上回るコード。



function detect_encoding_jp($str) {
$codes = array('JIS-ms' ,'eucJP-win', 'UTF-8','SJIS-win','UTF-16LE', 'UTF-16BE');
foreach ($codes as $code) {
do {
$flg = mb_check_encoding($str, $code);
if( $code=='UTF-16LE' || $code=='UTF-16BE') break;
$flg = $flg && !!mb_detect_encoding($str,$code, true);
} while(0);
if($flg) return $code; }
return NULL; }