I currently have a file such as this.
[Line 1] Hello
[Line 2] World
[Line 3] Hello World
I wish to look for all lines containing "Hello" which would be Line 1 and 3.
Then I would like to change on that line all cases of "Line" to "Changed" so the output would be
[Changed 1] Hello
[Line 2] World
[Changed 3] Hello World
- With line two being untouched. I currently have code to locate all lines with Hello in them, but am unsure how to edit those lines alone and no other.
For example the code below does find all the lines, but also deletes everything in the process with the str_replace, so I know it's not str_replace I'm seeking.
$lines = file("lines.html");
$find = "Hello";
$repl = "Changed";
foreach($lines as $key => $line)
if(stristr($line, $find)){$line = str_replace("$find","$repl",$line);}