判斷txt文件的字符編碼

最近有項目須要將txt文檔經過openoffice轉爲PDF,而後在經過swftools將PDF轉爲swf,在linux系統下的字符編碼方式爲utf-8,因此需將不是UTF-8的文本文檔轉換爲UTF-8php

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

轉載自:http://www.9958.pw/post/php_codelinux

相關文章
相關標籤/搜索