在php中编写编码为UTF-8的文件
PS:这不是一个重复的问题,因为我不想在文件中写入内容,因为它已经完成了,我想将文件的类型更改为UTF-8,有一个
如何生成 UTF-8 文件而不是 ANSI 文件。 (不是内容)。
例如,大多数 IDE 都有一个选项编码,您可以在其中修改文件的类型,但我从数据库生成大量文件,它会生成很多单独的文本文件,但整个文件是 ANSI默认..我只是在 php 中寻找一个函数,可以在生成批量之前更改编码。
如果源代码有帮助,我可以将其发布在这里。请告诉我。
提前致谢。
已编辑
按照我在此处询问的内容进行打印。
当我生成文件“testecli01.csv”时,它总是获得 ANSI 编码,无论我在脚本中做什么,它总是 ANSI,而我需要 UTF-8,就是这样。很简单,但我不知道该怎么做。
P.S.: It is not a duplicated question, because I'm not looking to write contents in a file because it is already done, I'm looking to change a type of a file to be UTF-8, there is a difference in it.
How to generate the UTF-8 file and not ANSI. (Is not the contents).
For example, the most IDE have an option encoding, where you are able to modify the type of your file, but I'm generating a bulk from my database, and it generates a lot of individual text files, but the whole files is ANSI default.. I'm just looking for a function in php that make it possible to change the encoding before it generates the bulk.
If the source code help I can post it here. just let me know.
Thanks in advance.
EDITED
Follow a print of what I'm asking here.
When I generate the file "testecli01.csv" it always get encoding ANSI, whatever I do in my script it is always ANSI, and I need in UTF-8, just this. Is simple but I have no idea how to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的第 3 方程序“不支持 ANSI 文件,但支持 UTF-8”,正如您在评论中提到的,那么很可能它期望 BOM。
因此严格来说,您的第 3 方程序并不完全符合标准,因为 BOM 应该是可选的。 ANSI 是 100% 有效的 UTF-8,这是它的主要驱动力之一。任何可以根据定义理解 UTF-8 标准的东西也可以理解 ANSI。
尝试将
"\xEF\xBB\xBF"
写入文件的前面,看看是否可以解决您的问题。If your 3rd party program "do not support files in ANSI but UTF-8" as you mentioned in a comment then most likely it's expecting a BOM.
So strictly speaking your 3rd party program isn't completely compliant with the standard because the BOM should be optional. ANSI is 100% valid UTF-8 and that is one of the main drivers of it. Anything that can understand UTF-8 accordng to the standard by definition also understands ANSI.
Try writing
"\xEF\xBB\xBF"
to the front of the file and see if that solves your problem.我不知道有哪个数据库可以轻松地为您完成编码转换。例如,在MySQL中,您必须重置数据库、表和列的所有字符编码,然后转换数据。
我建议您创建数据库转储并使用 iconv 来更改编码,无论是在命令行上
还是在 PHP 中(取自 如何以 UTF-8 格式写入文件?)
注意:进行编辑以避免泄漏文件描述符。
I do not know of a database that will do the encoding conversion for you easily. For example, in MySQL, you have to reset all the character encodings for the db, tables, and columns, AND THEN convert the data.
I would suggest instead that you create your database dump and use
iconv
to change the encoding, whether on the command line:or in PHP (taken from How to write file in UTF-8 format?)
NOTE: edited to avoid leaking file descriptors.
Excel 喜欢 CSV 文件为
UTF-16LE
,并以“\xFF\xFE
”开头。我为 Excel 构建文件的代码是:
Excel likes CSV files to be
UTF-16LE
, and begin with '\xFF\xFE
'.My code to build a file for excel is:
旧的编码是第一个,因为它在 iconv 函数中。
您也无法读取和写入同一文件。
Old encoding is first, as it is in iconv function.
You also can´t read and write same file.