使用PHP更改文件编码会生成换行符

时间:2012-02-29 15:13:20

标签: php batch-file cmd iconv

我使用PHP来更改文件编码,但它会在每行之后生成一个新的空行。

$t_comment = file_get_contents('php://stdin');
$t_comment = iconv("ISO-8859-1", "UTF-8//IGNORE",$t_comment);
echo $t_comment;

这是 myfile.txt

  

第1行 [CR] [LF]
  第2行 [CR] [LF]
  第3行 [CR] [LF]

转换命令(BATCH):

C:\> type myfile.txt | php.exe myscript.php

C:\> php.exe myscript.php < myfile.txt

结果:

  

第1行 [LF]
  的 [LF]
  第2行 [LF]
  的 [LF]
  line3 [LF]
  的 [LF]

你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

您有其他功能:utf8_encodemb_convert_encoding,虽然我已在本地测试iconv并且它可以正常运行,但我没有使用Windows。

考虑到您使用的是Windows并使用type生成PHP脚本输入,您应该检查活动控制台代码页,这可能会影响type命令的输出:{{3} }

首先,尝试通过将代码的第一行更改为以下内容来放弃使用iconv的问题,并查看会发生什么:

$t_comment = "line 1 \r\nline 2 \r\nline 3 \r\n";

答案 1 :(得分:0)

我发现由于双管命令生成了新行。所以我决定合并两个sripts只有一个管道。

所以我改变了:

C:\> type myfile.txt | php.exe myscript1.php | php.exe myscript2.php

为:

C:\> type myfile.txt | php.exe myscript1-merge2.php

这解决了问题,但没有回答额外换行的来源。