php保存文件为ansi,无法使用PHP将文件从ANSI转换为UTF-8(Unable to convert file from ANSI to UTF-8, using PHP)...

无法使用PHP将文件从ANSI转换为UTF-8(Unable to convert file from ANSI to UTF-8, using PHP)

我有一个文件,其中包含一些西里尔字符。 当我在Notepad ++中打开此文件时,我看到它具有ANSI编码。 如果我使用Notepad ++手动将其编码为UTF-8,那么一切都绝对正常 - 我可以在我的解析器中使用此文件并获得结果。 但我想要的是使用PHP以编程方式进行。 这是我在搜索SO和文档后尝试的内容:

file_put_contents($file, utf8_encode(file_get_contents($file)));

在这种情况下,当我的算法解析生成的文件时,它会遇到“è”,“í”,“â”等字母。 换句话说,在这种情况下,我得到了一些垃圾。 我也试过这个:

file_put_contents($file, iconv('WINDOWS-1252', 'UTF-8', file_get_contents($file)));

但它产生了同样的垃圾。 所以,我真的很想知道如何以编程方式实现Notepad ++的功能。 谢谢!

I have a file, which contains some cyrillic characters. When I open this file in Notepad++ I see, that it has ANSI encoding. If I manually encode it into UTF-8 using Notepad++, then everything is absolutely ok - I can use this file in my parsers and get results. But what I want is to do it programmatically, using PHP. This is what I tried after searching through SO and documentation:

file_put_contents($file, utf8_encode(file_get_contents($file)));

In this case when my algorithm parses the resulting files, it meets such letters as "è", "í" , "â". In other words, in this case I get some rubbish. I also tried this:

file_put_contents($file, iconv('WINDOWS-1252', 'UTF-8', file_get_contents($file)));

But it produces the very same rubbish. So, I really wonder how can I achive programmatically what Notepad++ does. Thanks!

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

更新时间:2019-11-27 09:11

最满意答案

Notepad ++可能会将您的编码报告为ANSI,但这不一定等同于Windows-1252。 1252是拉丁字母的编码,而1251是为了编码西里尔字母。 所以用

file_put_contents($file, iconv('WINDOWS-1251', 'UTF-8', file_get_contents($file)));

使用iconv从1251转换为utf-8。

Notepad++ may report your encoding as ANSI but this does not necessarily equate to Windows-1252. 1252 is an encoding for the Latin alphabet, whereas 1251 is designed to encode Cyrillic script. So use

file_put_contents($file, iconv('WINDOWS-1251', 'UTF-8', file_get_contents($file)));

to convert from 1251 to utf-8 with iconv.

2017-02-18

相关问答

关于这一部分: 当我把它转换成UTF-8没有bom和关闭文件,当我重新打开文件再次是ANSI。 最简单的解决方案是通过正确配置记事本++来完全避免问题。 尝试Settings - > Preferences - > New document - > Encoding - >选择不带BOM的UTF-8 ,然后选中Apply to opened ANSI files 。 这样所有打开的ANSI文件将被视为没有BOM的UTF-8。 为了解释发生了什么,请阅读这个答案下面的意见。 要充分了解Unicode

...

Windows-1252是拉丁语编码; 你不能编码Windows-1252中的希伯来字符。 这就是为什么它不起作用。 Windows-1255是希伯来语的编码,这就是它的工作原理。 它不能与mb_convert_encoding一起使用的mb_convert_encoding是mb_不支持Windows-1255 。 根据定义, 检测编码是不可能的。 Windows-1255是单字节编码; 几乎不可能区分任何一个字节编码与另一个字节编码。 结果与ASCII中的有效一样,与Windows-1255或

...

$tmp = iconv('YOUR CURRENT CHARSET', 'UTF-8', $string);

要么 $tmp = utf8_encode($string);

奇怪的是,你最终会在数据库中出现一个空字符串。 我可以理解你最终会在你的数据库中添加一些garbarge,但没有任何内容(空字符串)很奇怪。 我只是在我的控制台中输入了这个信息: iconv -l | grep -i ansi

它向我显示: ANSI_X3.4-1968

ANSI_X3.4-1986

ANSI_X3.4

...

问题是您将初始文件保存为ASCII(您将CreateTextFile()的Unicode参数设置为False)。 根据文件 : object.CreateTextFile(filename [,overwrite [ ,unicode ]]) CreateTextFile方法包含以下部分: 部分说明 所需对象。 始终是FileSystemObject或Folder对象的名称。 文件名必需。 用于标识要创建的文件的字符串表达式。 覆盖可选。 布尔值,指示是否可以覆盖现有文件。 如果文件可以被覆盖,则

...

VBA的Open函数仅适用于ANSI编码文件和二进制文件。 如果你想读/写utf-8文件,你必须找到另一种方法。 utf-8编码具有比ANSI更大的字符集,因此不可能毫无损失地从ANSI转换为utf-8 。 也就是说,Excel和VBA中的String存储为utf-16 (VBA编辑器仍然使用ANSI ),因此您只需要将utf-8转换为utf-16 。 使用ADODB.Stream : Public Function ReadFile(path As String, Optional CharSe

...

删除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

...

问题是你永远不会解码你编码的数据。 use strict;

use warnings;

use Encode qw( encode decode );

open(my $INFILE, '

open(my $OUTFILE, '>', $ARGV[1]) or die $!;

while (my $utf8 = ) {

my $code_points = decode('UTF-8', $utf8); #

...

听起来这是一个BOM问题而不是编码问题。 您可以在编写文件时删除任何BOM字符,其中包括: line = line.replace("\ufeff", "");

这就留下了一个问题,即你是否正在准确地读取数据...我强烈建议你不要使用FileWriter和FileReader - 而是使用InputStreamReader和OutputStreamWriter ,明确指定两者的编码。 将读取器编码设置为UTF-8(假设输入文件确实是UTF-8),并将编写器编码设置为您想要的任何...但是我建议坚

...

如果文件具有UTF-8 BOM,则很容易,使用LoadStringsFromFile加载文件,使用SaveStringsToFile将其保存回Ansi编码: function ConvertFileFromUTF8ToAnsi(FileName: string): Boolean;

var

Lines: TArrayOfString;

begin

Result :=

LoadStringsFromFile(FileName, Lines) and

SaveStringsTo

...

Notepad ++可能会将您的编码报告为ANSI,但这不一定等同于Windows-1252。 1252是拉丁字母的编码,而1251是为了编码西里尔字母。 所以用 file_put_contents($file, iconv('WINDOWS-1251', 'UTF-8', file_get_contents($file)));

使用iconv从1251转换为utf-8。 Notepad++ may report your encoding as ANSI but this does not n

...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值