php utf-8文件,在php中编写一个带有UTF-8编码的文件(Write a file with encoding UTF-8 in php)...

在php中编写一个带有UTF-8编码的文件(Write a file with encoding UTF-8 in php)

PS:这不是一个重复的问题,因为我不打算在文件中写内容,因为它已经完成了,我想将一个文件类型更改为UTF-8,它有区别。

如何生成UTF-8文件而不是ANSI。 (不是内容)。

例如,大多数IDE都有一个选项编码,您可以在其中修改文件的类型,但我从我的数据库生成批量,它会生成大量单独的文本文件,但整个文件是ANSI默认..我只是在php中寻找一个函数,可以在生成批量之前更改编码。

如果源代码帮助我可以在这里发布。 请告诉我

提前致谢。

EDITED

按照我在这里要求的印刷品。

dccvY.png

当我生成文件“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.

dccvY.png

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.

原文:https://stackoverflow.com/questions/6629430

更新时间:2020-02-04 09:44

最满意答案

如果您的第三方程序“不支持ANSI中的文件但UTF-8”,正如您在评论中提到的那样,那么很可能它会期待BOM 。

虽然Unicode标准确实允许使用UTF-8的BOM,[2]但它不需要或推荐它。[3] 字节顺序在UTF-8 [4]中没有意义,因此BOM仅用于将文本流或文件标识为UTF-8。

建议使用BOM的原因是它会破坏作为UTF-8设计一部分的ASCII反向兼容性。

严格来说,您的第三方程序并不完全符合标准,因为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.

While the Unicode Standard does allow a BOM in UTF-8,[2] it does not require or recommend it.[3] Byte order has no meaning in UTF-8[4] so a BOM serves only to identify a text stream or file as UTF-8.

The reason the BOM is recommended against is that it defeats the ASCII back-compatibility that is part of UTF-8's design.

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.

2011-07-11

相关问答

header('Content-type: text/plain; charset=utf-8');

header('Content-type: text/plain; charset=utf-8');

您必须确保发出的sed命令是UTF-8编码的。 您可以通过以下两种方式之一完成此操作: 将sed命令写入文件,确保文件为UTF-8,并将其作为脚本执行: file yourfile应该说UTF-8 Unicode text 。 然后,您可以运行bash yourfile 。 或者,将终端和shell设置更改为UTF-8 printf à | wc -c printf à | wc -c必须说2,而不是1。 locale应在LC_CTYPE行中列出“UTF-8”或“utf8”。 然后,您可以直接从终

...

您还应确保使用UTF-8编码保存db.php文件。 You should also make sure that the db.php file is saved with UTF-8 encoding.

看起来,MySQL(php)客户端确实承担了另一种客户端编码,而不是UTF-8。 您可以使用请求正确的编码 $dbhconnect->exec("SET NAMES utf8");

直接连接到数据库后。 或者,您可以在连接配置中使用PDO :: MYSQL_ATTR_INIT_COMMAND(请参阅http://php.net/manual/de/ref.pdo-mysql.php ): $dbconnect = new PDO("mysql:host=$host;dbname=$databas

...

删除BOM,然后执行: $file = file_get_contents('file.php');

$file = iconv('greek-charset','UTF-8', $file);

file_put_contents('file.php', $file);

//ta-da!

更改greek-charset以更正greek-charset名称(可能是Windows-1253 )。 Remove BOM, then do: $file = file_get_contents('file

...

首先,不要依赖于mb_detect_encoding 。 除非有一堆编码特定实体(意味着在其他编码中无效的实体),否则在确定编码是什么方面并不是很好。 尝试一起摆脱mb_detect_encoding行。 哦, utf8_encode将Latin-1字符串转换为UTF-8字符串(不是从任意字符集到UTF-8 ,这是你真正想要的)...你想要iconv ,但你需要知道源编码(因为你不能真正信任mb_detect_encoding ,你需要通过其他方式来解决它。 或者你可以尝试使用带有空输入编码的$s

...

在Vim中有很多与编码有关的混淆。 有两种编码设置, 'encoding'和'fileencoding' 。 'encoding'是与当前vim会话相关的一个 - 我将它始终保留为'utf-8',但后来我只使用gVim或启用了unicode的终端。 'fileencoding'是文件本身的编码,可以自动检测到,或者可以用设置( ++enc )或模式行覆盖,我相信。 它基于'fileencodings'选项进行检测。 尝试这个: vim

:set encoding=utf-8

:e ++enc=ut

...

如果您的第三方程序“不支持ANSI中的文件但UTF-8”,正如您在评论中提到的那样,那么很可能它会期待BOM 。 虽然Unicode标准确实允许使用UTF-8的BOM,[2]但它不需要或推荐它。[3] 字节顺序在UTF-8 [4]中没有意义,因此BOM仅用于将文本流或文件标识为UTF-8。 建议使用BOM的原因是它会破坏作为UTF-8设计一部分的ASCII反向兼容性。 严格来说,您的第三方程序并不完全符合标准,因为BOM应该是可选的。 ANSI是100%有效的UTF-8,这是它的主要驱动因素之一。

...

您需要告诉它搜索主题是UTF8。 function checkEmail($email) {

if (!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/u" , $email)) {

return false;

}

return true;

}

在那个正则表达式中,我在结尾处添加了u ,这使得模式和字符串

...

你需要HTML实体编码吗? 功能 function utf8_to_escaped_another($str, $encoding) {

$utf8 = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);

$out = $utf8;

mb_convert_variables($encoding, 'UTF-8', $out);

$table = get_html_translation_table(HTML_SPECI

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值