問題描述
我正在使用 php fwrite() 創建一個文件,我知道我的所有數據都在 UTF8 中(我已經對此進行了廣泛的測試 - 將數據保存到 db 并在普通網頁上輸出時一切正常并報告為 utf8.),但我被告知我輸出的文件包含非 utf8 數據:( 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?
當使用 vim 時,它顯示的內容為:
When using vim it shows the content as:
Dona~@~Yt 做任何事.... Ita~@~Ys很棒的網站一切....Wea~@~Yve 只是啟動/
Dona~@~Yt do anything .... Ita~@~Ys a great site with everything....Wea~@~Yve only just launched/
任何幫助將不勝感激:確認文件是 UTF8 或如何將 utf8 內容寫入文件.
Any help would be appreciated: Either confirming the file is UTF8 or how to write utf8 content to a file.
更新
為了闡明我如何知道我有 UTF8 格式的數據,我做了以下工作:
To clarify how I know I have data in UTF8 i have done the following:
- DB 設置為 utf8 保存數據時
到數據庫我先運行這個:
- 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);
就在我運行 fwrite 之前,我已經檢查了數據 注意每條數據返回 '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 中添加一個 UTF8 BOM,數據是正確的但文件閱讀器(外部應用程序)無法讀取沒有 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
這篇關于fwrite() 和 UTF8的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!