Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change just one line of a Text file?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How can i change just one line of an text file?
like i hvae a text file containing:
red
green
blue
black
How can i then change green to brown?
 
Yes sure, it's not too difficult:
Code:
<?
$lines=file(&quot;file.txt&quot;);
for ($i=0; $i<count($lines); $i++)
{
  if ($lines[$i]==&quot;green\n&quot;) // watch the \n !!
  {
    $lines[$i]=&quot;brown\n&quot;; // there it is again!
    break; // if you want to continue with other changes
           // don't use break;
  }
}

$fp=fopen(&quot;file.txt&quot;, &quot;w&quot;);
fwrite($fp, implode(&quot;&quot;, $lines));
fclose($fp)
?>

i didn't test it, so if it's not working please reply or get the bug out yourself...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top