function chkCode($string){ $code = array('UTF-8','GBK','GB18030','GB2312'); foreach($code as $c){ if( $string === iconv('UTF-8', $c, iconv($c, 'UTF-8', $string))){ return $c; } } return "no"; } $file1 = 'test1.txt'; $str1 = file_get_contents($file1); echo chkCode("$str1");