Php 打印出修改文件的内容

Php 打印出修改文件的内容,php,Php,我在用php输出修改过的文件时遇到问题,下面是我的代码: <?php $content = file_get_contents("1.txt"); $items = explode("\n",$content); $line = ""; foreach ($items as $item){ $values = explode(",",$item); foreach ($values as &$value){ $key = array_search($value,$valu

我在用php输出修改过的文件时遇到问题,下面是我的代码:

<?php
$content = file_get_contents("1.txt");
$items = explode("\n",$content);
$line = ""; 
foreach ($items as $item){
$values = explode(",",$item);
foreach ($values as &$value){
    $key = array_search($value,$values);
    if ($key!=4){
        $line .= $value.",";
    }
    elseif ($value=="0"){
        $value="?";
        $line .= $value."\n";
    }
    else $line .= $value."\n";
}
}
$file = fopen("1.txt","w");
fwrite($file,$line);
fclose($file); 
?>
所需的输出是

1,1,1,1,1
2,2,2,2,?
但是,我在运行脚本时得到了以下信息:

1,1,1,1,1,2,2,2,2,?
,

我的剧本有什么问题?非常感谢

您没有正确地将线路连接到
\n
。以下是我的位修改函数:

$content = '1,1,1,1,1
2,2,2,2,0';

$lines = explode("\n", $content);

$modLines = array();

foreach ($lines as $line)
{
    $values = explode(',', $line);

    foreach($values as &$value)
    {
        # Do here what ever you want to
         if($value == '0')
        $value = '?';
    }

    $modLines[] = implode(',', $values);

}

$content = implode("\n", $modLines);

echo $content;
返回

1,1,1,1,1
2,2,2,2,?

+1对于Robik,无论如何,我刚刚测试了你的原始脚本,它对我来说很好。也许你想读《谢谢你,罗比克》,它确实有帮助!
1,1,1,1,1
2,2,2,2,?