设置utf-8编码以读取fwrite [英] set utf-8 encoding for fread fwrite

查看:406
本文介绍了设置utf-8编码以读取fwrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用此代码在file中读取和写入文本.

hi i use this code read and write text in file .

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

$d = fopen("chat.txt", "a");
 $c=$_GET['c'];
 if($c=='') die();
 fwrite($d,$c.'||');
 fclose($d);

但在= ie only = utf-8字符中显示?"或者 "[]" .我的无Utf-8编码的BOM 我用这个

but in =ie only= utf-8 character show "?" or "[]" . my encoding Utf-8 Without BOM and i use this

header('Content-type: text/html; charset=UTF-8');

和此:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

我在php.ini中的defult编码是utf-8,但是还显示吗? . 我在文件中看到chat.txt文件和字符,但是当与ie一起保存在文件中时以及在页面中显示时显示?"而不是正确的.

my defult encoding in php.ini is utf-8 but yet show ? . i see chat.txt file and character right in file but when with ie save in file And when show in page show "?" instead of right .

推荐答案

在读取时使用此函数而不是fopen,而在写入时则不要

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

来源
http://www.php.net/manual/zh/function.fopen .php#104325

您的情况

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

尝试一下

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

这篇关于设置utf-8编码以读取fwrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆