2012-05-18 108 views
4

嗨,我使用此代碼讀取和寫入文件中的文本。爲fread fwrite設置utf-8編碼

$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瀏覽器僅= UTF-8字符秀 「?」要麼 」[]」 。我編碼UTF-8無BOM 我用這個

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保存在文件中,當在頁面中顯示顯示「?」而不是正確的。

+0

對於每一個($萬億爲$萬億)改寫$億,所以它只能與BN1,但沒有diferent –

+0

變化億BOM –

回答

0

TXT保存爲utf8編碼?你需要確保TXT編纂是utf-8,否則你將需要使用函數utf8_encode功能

+0

是UTF-8或UTF-8沒有執行它像一個heigharcy –

+0

我使用utf8不工作,並更改爲ansi使用utf8_encode,但不工作 –

+0

您可以發佈不正確的字符輸出嗎? –

0

您可以編碼在這種情況下,你輸出字符串,$億,使用函數utf8_encode()這樣的:

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

試試看看它是否仍然很奇怪。

+0

not worked show? 不是「?」 –

+0

如果你看到這樣的事情,那麼最可能的解決辦法是確保你正在編寫的文件的內容是utf8編碼的。另外,請查看@Venu上面的答案。字符編碼問題總是一個麻煩來解決。祝你好運。 – EmmanuelG

10

使用此功能,而不是的fopen一邊看書而不是在寫

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/en/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>'; 
+0

沒有更多的滾動調試會話...對不起。 – casperOne