为什么它在下一行的开头添加文本,而不是在右一行的末尾添加文本?

Why does it add the text at the beginning of next line, and not the end of the right one?

需要解决方案:

我正在使用一个简单的 PHP 脚本,它应该:

  1. 在文件第一行末尾添加"value4:::"
  2. 在所有下一行的末尾添加“:::”

我是编程新手,坐在这里两天试图解决这个问题。可能是一个小细节,也可能是执行此任务的完全错误的方法。

这样做可能是错误的方法,或者可能是正则表达式问题?我真的不知道。

恳请您帮我解决问题。


信息:

文件 newstest.db 如下所示:

ID:::value1:::value2:::value3:::
1:::My:::first:::line:::
2:::My:::second:::line:::
3:::Your:::third:::line:::

使用这个 php 脚本我想让它看起来像这样:

ID:::value1:::value2:::value3:::value4:::
1:::My:::first:::line::::::
2:::My:::second:::line::::::
3:::Your:::third:::line::::::

问题:

到目前为止我差不多明白了,但我很困惑为什么它会在 second[=] 的 beginning 后面加上 "value4:::" 70=] 行,然后在所有其余行的开头(不是结尾)添加“:::”。

所以我得到一个如下所示的文件:

ID:::value1:::value2:::value3:::
value4:::1:::My:::first:::line:::
:::2:::My:::second:::line:::
:::3:::Your:::third:::line:::

我认为:

$lineshere = 'Some text here:::';    
$lines1 = $linehere.'value4:::';

会输出"Some text here:::value4:::"

问题可能是由于这种逐行添加的方式造成的吗?

$lines = '';
$lines.= 'My test';
$lines.= ' is here';
echo $lines;

我是编程新手,所以我可能使用了完全错误的函数 tec 来完成这项工作。

但在这种情况下,它会在错误的位置添加 space 或换行符/换行符。


我对这个解决方案的尝试:

<?php
// specify the file
$file_source="newstest.db";

// get the content of the file
$newscontent = file($file_source, true);

//set a start value (clear memory)
$lines ='';

// get each line and treat it line by line. 

foreach ($newscontent as $line_num => $linehere) {  

    // add "value4:::" at the end of FIRST line only, and put it in memory $lines 
    if($line_num==0) {
        $lines.= $linehere.'value4:::';

        // Just to see what the line looks like
        //echo 'First line: '.$lines.'<br /><br />';
    }

    // then add ":::" to the other lines and add them to memory $lines
    if($line_num>0) {
        $lines1 = $linehere.':::';
        $lines.= $lines1;

        //just look at the line
        //echo 'Line #'.$line_num.': '.$lines1.'<br /><br />';
    }
}

//Write new content to $file_source
$f = fopen($file_source, 'w');
fwrite($f,$lines);
fclose($f);

echo "// to show the results ar array<br /><br />";

$newscontentlook = file($file_source, true);
print_r(array_values($newscontentlook));
?>

函数 file() 为您提供数组中的文件,每一行都是数组的一个元素。行的每个结尾 (EOL) 都包含在元素中。因此,将文本附加到该行将在 EOL 之后,因此有效地在下一行的开头。

您可以选择不使用 file() 并在 EOL 上自行分解文本,因此 EOL 不再是数组元素的一部分。然后编辑元素,再次内爆数组。

fopen()文件,然后fread()自己通过它。后一种选择是首选,因为它不会立即将整个文件加载到内存中。

我认为问题出在你通过 file() 函数读取的行的循环中:

foreach ($newscontent as $line_num => $linehere) {
  ...

$linehere 末尾包含一个换行符,因此您应该在使用前将其简单地切掉:

foreach ($newscontent as $line_num => $linehere) {
  $linehere = chop($linehere);
  ...

如果您不 chop 行内容,当您将字符串连接到它时,您将得到:

LINE_CONTENTS\nSTRING_ADDED

打印时将是:

LINE_CONTENTS
STRING_ADDED

希望这对您有所帮助...

这实际上很容易用 file_get_contents and preg_replace 实现,即:

$content = file_get_contents("newstest.db");
$content = preg_replace('/(^ID:.*\S)/im', 'value4:::', $content);
$content = preg_replace('/(^\d+.*\S)/im', ':::', $content);
file_put_contents("newstest.db", $content);

输出:

ID:::value1:::value2:::value3:::value4:::
1:::My:::first:::line::::::
2:::My:::second:::line::::::
3:::Your:::third:::line::::::

Ideone Demo