<meta charset='utf-8'> <?php $res=glob('*.txt'); Read the txt file inside the folder echo 'directly output file name, Chinese is garbled, because this php script is encoded as utf-8:</br></br>'; foreach($res as $value)
{ echo $value.'</br>';
} echo '</br>The following is encoded with the iconv function:</br></br>'; foreach($res as $value)
{ echo iconv('gb2312','utf-8',$value).'</br>'; The first parameter is the encoding of the input data, that is, the encoding of the file name we read, and the second parameter is the encoding of the output data, which is now displayed on our web page
//的编码,为utf-8
} echo '</br>Output file content directly: txt text content saved as asc format garbled because php script is in utf-8 format</br></br>'; foreach($res as $value)
{ echo iconv('gb2312','utf-8',$value).' The content is: '.file_get_contents ($value).''</br>;
} echo '</br></br>The following is encoded with the iconv function:</br></br>'; foreach($res as $value)
{ $str=file_get_contents($value); $isutf8=mb_check_encoding($str,'utf-8'); Check if the text content is encoded in line with the web page if($isutf8!='utf-8')
{ echo iconv('gb2312','utf-8',$value).' The content is: '.iconv('gb2312','utf-8',file_get_contents($value)).'</br>'; If there is inconsistency, it will be converted
} else
{ echo iconv('gb2312','utf-8',$value).' The content is: '.file_get_contents ($value).''</br>;
}
} ?> |