fwrite()和UTF8 [英] fwrite() and UTF8

查看:214
本文介绍了fwrite()和UTF8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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 a 很棒的网站 一切....我们只是 启动/

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.

更新

UPDATE

为了阐明我如何知道我在UTF8中有数据,我做了以下工作:

To clarify how I know I have data in UTF8 i have done the following:

  1. 保存数据时将数据库设置为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')) print 'NOT UTF-8'; else print 'IS utf-8';

if (strlen($data)==mb_strlen($data, 'UTF-8')) print 'NOT UTF-8'; else print 'IS utf-8';

谢谢!

推荐答案

我唯一要做的就是将UTF8 BOM添加到CSV中,数据是正确的,但是文件读取器(外部应用程序)无法读取没有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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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