Editing a text file using PHP
Moderator: General Moderators
Editing a text file using PHP
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.
I looked at the manual, and I tried some code, but it didnt work.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You can loop through the array made by file() and search and replace as needed.
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
This snippet made use of str_replace(), but much more advanced replacing like preg_replace() could've been coded.
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);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.