fwrite() 和 UTF8

2021-12-28 00:00:00 utf-8 php fwrite

我正在使用 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:

Donâ~@~Yt 做任何事.... Itâ~@~Ys很棒的网站一切....Weâ~@~Yve 只是启动/

Donâ~@~Yt do anything .... Itâ~@~Ys a great site with everything....Weâ~@~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:

  1. DB 设置为 utf8 保存数据时
  2. 到数据库我先运行这个:

  1. DB is set to utf8 When saving data
  2. 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

相关文章