set utf-8 encoding for fread fwrite

可紊 提交于 2019-11-30 23:45:39

Use this function instead of fopen while reading but not while writing

function utf8_fopen_read($fileName) { 
    $fc = iconv('windows-1250', 'utf-8', file_get_contents($fileName)); 
    $handle=fopen("php://memory", "rw"); 
    fwrite($handle, $fc); 
    fseek($handle, 0); 
    return $handle; 
} 

source
http://www.php.net/manual/en/function.fopen.php#104325

In your case

$d = utf8_fopen_read("chat.txt", "r");
 $content=fread($d,filesize('chat.txt'));
 $bn=explode('||',$content);
 foreach($bn as $bn)
 echo $bn.'<br>';

Try this

$content = iconv('windows-1250', 'utf-8', file_get_contents($fileName));
$bn = mb_split('||',$content);
foreach($bn as $b)
     echo $b.'<br>';

The TXT was saved with utf8 encode? You need to ensure that the TXT codification is utf-8, otherwise you will need use utf8_encode function

You can encode the string you are outputting, $bn in this case, using utf8_encode() like this:

$d = fopen("chat.txt", "r");
$content=fread($d,filesize('chat.txt'));
$bn=explode('||',$content);
foreach($bn as $bn)
echo utf8_encode($bn).'<br>';

Try that and see if it's still wierd.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!