問題描述
我正在使用 php fwrite() 創(chuàng)建一個(gè)文件,我知道我的所有數(shù)據(jù)都在 UTF8 中(我已經(jīng)對(duì)此進(jìn)行了廣泛的測(cè)試 - 將數(shù)據(jù)保存到 db 并在普通網(wǎng)頁(yè)上輸出時(shí)一切正常并報(bào)告為 utf8.),但我被告知我輸出的文件包含非 utf8 數(shù)據(jù):( bash (CentOS) 中是否有命令可以檢查文件格式?
I am creating a file using php fwrite() and I know all my data is in UTF8 ( I have done extensive testing on this - when saving data to db and outputting on normal webpage all work fine and report as utf8.), but I am being told the file I am outputting contains non utf8 data :( Is there a command in bash (CentOS) to check the format of a file?
當(dāng)使用 vim 時(shí),它顯示的內(nèi)容為:
When using vim it shows the content as:
Dona~@~Yt 做任何事.... Ita~@~Ys很棒的網(wǎng)站一切....Wea~@~Yve 只是啟動(dòng)/
Dona~@~Yt do anything .... Ita~@~Ys a great site with everything....Wea~@~Yve only just launched/
任何幫助將不勝感激:確認(rèn)文件是 UTF8 或如何將 utf8 內(nèi)容寫入文件.
Any help would be appreciated: Either confirming the file is UTF8 or how to write utf8 content to a file.
更新
為了闡明我如何知道我有 UTF8 格式的數(shù)據(jù),我做了以下工作:
To clarify how I know I have data in UTF8 i have done the following:
- DB 設(shè)置為 utf8 保存數(shù)據(jù)時(shí)
到數(shù)據(jù)庫(kù)我先運(yùn)行這個(gè):
- DB is set to utf8 When saving data
to database I run this first:
$enc = mb_detect_encoding($data);
$data = mb_convert_encoding($data, "UTF-8", $enc);
就在我運(yùn)行 fwrite 之前,我已經(jīng)檢查了數(shù)據(jù) 注意每條數(shù)據(jù)返回 'IS utf-8'
Just before I run fwrite i have checked the data with Note each piece of data returns 'IS utf-8'
if (strlen($data)==mb_strlen($data, 'UTF-8')) 打印 '非 UTF-8';否則打印 'IS utf-8';
謝謝!
推薦答案
我唯一要做的就是在 CSV 中添加一個(gè) UTF8 BOM,數(shù)據(jù)是正確的但文件閱讀器(外部應(yīng)用程序)無法讀取沒有 BOM 的正確文件
The only thing I had to do is add a UTF8 BOM to the CSV, the data was correct but the file reader (external application) couldn't read the file properly without the BOM
這篇關(guān)于fwrite() 和 UTF8的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!