簡體   English   中英

打印出修改過的文件的內容

[英]print out the content of a modified file

我在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.txt中的原始內容是

1,1,1,1,1
2,2,2,2,0

期望的輸出是

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,?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM