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!

Replacing a string within a text file

Status
Not open for further replies.

Azathoth

Technical User
Jul 14, 2003
61
US
How would I go about replacing a string in an existing text file? For example, the file example.txt contains:

Item1
Item2
Item3

I have no problem iterating through the file and getting each line value, but is there some way to say, replace the value "Item2" with "newItem2" without touching the rest of the file?

Thanks in advance.
 
Only if you know in advance where "Item2" is, otherwise you have to find it.

Even then, if you write 8 bytes over a 5-byte string, you're going to have problems. You'll end up with something like

Item1
Item2m3



 
Ok, how about parsing the file, taking each value that I don't want to change, and re-writing them? Then of course, taking the one that I do want to change and altering it? Can this all be done within the same file?
 
It could possibly be done with a single file. Read the file into memory, edit what you need, write the file back.

But I wouldn't recommend it unless you know for a fact that the file is going to remain small.

Another method is to stream the data from one file to another, making changes to the data stream as you go. Then delete the original file and rename the new file to the old file's name.

Another big "gotcha" with file editing through a web script is that a web script always has the possibility of being a multiuser application. Your script must be aware of its other instantiations trying to make changes to the same single file. This is often taken care of through file locking:
Another possibility is to store your data in a server-based database. Your code doesn't have to manipulate the file, only send queries to a database, and the database server will take care of multiple script instantiations trying to edit data simultaneously.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I worked on something quite similar recently. This will do exactly what you described. Of course you have to define exactly what you're replacing but if that is going to change you can put it into a $_POST variable

Code:
//read the text file, define what text you're replacing, GET or POST new text, str_replace the specified string
$text_file = file_get_contents("example.txt");
$text_to_replace = "Item2";
$new_text = "whatever you want I'm guessing a $_POST";
$final_data = str_replace($text_to_replace, $new_text, $text_file);

then of course you will want to write this back to the file
Code:
//Opens txt file in write mode
$open_example_txt = fopen("example.txt", "w");

//open and implode, write the new info, close the file
fputs($open_example_txt,implode,("\n"));
fwrite($open_example_txt,"$final_data");
fclose($open_example_txt);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top