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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file search find and replace

Status
Not open for further replies.

csbdeady

Programmer
May 18, 2002
119
GB
Hi there

This has been discussed before but I'm afraid I lost the plot and couldn't work out how to proceed:(

I have a text file, eg:

a
b
c

I need to be able to search the text file for (eg) "b" and replace with "d" thus ending up with a saved text file of:

a
d
c

What's the easiest way to do this please?

I've thought of loading the file line by line and checking the currently loaded line against a variable and if they match then write at that point into the file after deleting the line... but again I'm lost on this.

To be honest I'd be happy if the output was:

a
c
d

Thanks once again!

-Colin :)
 
This code should work:
<?php
$fp = fopen($filename, &quot;r+&quot;);
$content = fread($fp, filesize($filename));
rewind($fp);
$content = preg_replace(&quot;/b/&quot;, &quot;d&quot;, $content);
fwrite($fp, $content);
?> //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top