Preben Preben - 7 months ago 8
PHP Question

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

Needed solution:

I am working with a simple PHP script that should:


  1. Add "value4:::" at the end of the first line in a file

  2. Add ":::" at the end of all the next lines



I am novise in programming, and have sit here for two days trying to figure this out. Might be a little detail, or it might be totally wrong way of doing this task.

It might be wrong way to do this, or may be a regex problem? I really don't know.

I kindly ask you to help me with the solution.




Information:

The file newstest.db looks like this:

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


And using this php script I'd like to make it look like this:

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





Problem:

So far I have almost got it, but I am confused to why it adds the "value4:::" to the beginning of the second line, and then add ":::" at the beginning (not end) of all of the rest of the lines.

So I get a file that looks like this:

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


I thought that:

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


would output "Some text here:::value4:::"

May be the problem is due to this way of adding line after line?

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


I am a novise in programming, so I might have used totally wrong functions tec to make this work.

But in this case it seams to add a space or line break/ line end in the wrong place.




My try on this solution:

<?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));
?>

Answer

This is actually very easy to achieve with file_get_contents and preg_replace, i.e:

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

Output:

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

Ideone Demo