Editing a text file using PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cursed
Forum Newbie
Posts: 16
Joined: Sat Aug 26, 2006 10:18 pm

Editing a text file using PHP

Post by cursed »

How would you find a line on a text file using php, then editing it?
I looked at the manual, and I tried some code, but it didnt work.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

use the function file() to make an array of the file you want then take the line number you want to edit and edit it. then you can save it with fwrite() and whatnot.
cursed
Forum Newbie
Posts: 16
Joined: Sat Aug 26, 2006 10:18 pm

Post by cursed »

is it possible not to use the line number? some replace function?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You can loop through the array made by file() and search and replace as needed.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You can also get the entire contents into a single string, ready for replacing.. by using file_get_contents()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
cursed
Forum Newbie
Posts: 16
Joined: Sat Aug 26, 2006 10:18 pm

Post by cursed »

i tried scottayy's way, but it didnt work.

can someone post a short code snippet? :oops:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Tell you what, post what you have and let us help you, instead of us doing it for you.
cursed
Forum Newbie
Posts: 16
Joined: Sat Aug 26, 2006 10:18 pm

Post by cursed »

never mind, i got it to work. Thanks for the help anyways. :D
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Well for other people reading the topic who may be curious..

If you're just looking to replace some text in a file, something like the following would work.

Code: Select all

$file = file_get_contents('somefile.txt');
$file = str_replace('data you\'re looking for', 'data to replace it', $file);

$handle = fopen($file, "w");
fwrite($handle,$file);
fclose($handle);
This snippet made use of str_replace(), but much more advanced replacing like preg_replace() could've been coded.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply