判断txt文件的字符编码

最近有项目需要将txt文档通过openoffice转为PDF,然后在通过swftools将PDF转为swf,在linux系统下的字符编码方式为utf-8,所以需将不是UTF-8的文本文档转换为UTF-8

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");