donglusou3335 2012-02-29 15:13
浏览 33

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

I use PHP to change file encoding, but it generates a new empty line after each line.

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

Here is myfile.txt:

line1 [CR][LF]
line2 [CR][LF]
line3 [CR][LF]

Conversion command (BATCH):

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

or

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

Result:

line1 [LF]
[LF]
line2 [LF]
[LF]
line3 [LF]
[LF]

Can you help me to fix this?

  • 写回答

2条回答 默认 最新

  • dsl36367 2012-02-29 15:59
    关注

    You have alternative functions: utf8_encode and mb_convert_encoding, although I have tested iconv locally and it works, but I am not using Windows.

    Considering you are on Windows and using type to generate the PHP script input, you should check the active console code page, which may be affecting the output of the type command: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/chcp.mspx?mfr=true

    First, try to discard the problem being with iconv by changing the first line of your code to the following, and seeing what happens:

    $t_comment = "line 1 
    line 2 
    line 3 
    ";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度