This article is a mirror article of machine translation, please click here to jump to the original article.

View: 8888|Reply: 0

PHP solution to read Chinese garbled characters in txt files

[Copy link]
Posted on 2/3/2015 4:25:15 PM | | |

<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>;
}
}
?>




Previous:Solutions to IIS 7.0 Error IIS Status Codes: IIS Detailed Error Codes and Explanations
Next:Storage architecture competition to see who will become the king of the big data era
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com