2)PHP中把读取.txt中内容并转为UTF-8格式
1 <?php 2 3 $filename = "filename.txt"; 4 $handle = fopen($filename, "r");//读取二进制文件时,需要将第二个参数设置成'rb' 5 //通过filesize获得文件大小,将整个文件一下子读到一个字符串中 6 $contents = fread($handle, filesize ($filename)); 7 $e=mb_detect_encoding($contents, array('UTF-8', 'CP936')); 8 switch($e){ 9 case 'UTF-8': 10 break; 11 case 'CP936':$contents=iconv("GB2312", "UTF-8", $contents); 12 break; 13 } 14 fclose($handle); 15 16 ?>