By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,363 Members | 692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,363 IT Pros & Developers. It's quick & easy.

Editing and modifying an existing text file

ganesanji
P: 16
hi to all,


I am new to php. I have to edit a text file using php. I saw the file system concepts modes. My problem is I want to change a particular text or word in a text file. How to find the index of the specific word. Is there any functions or methods available for change a particular word or finding index of a word...
For the example, consider a text file named ganesh.txt which content is shown below....
ganesh
is
working
in
php.



The ganesh.txt file conent is shown above ended with php.In that file I want to change the words "ganesh" to "Dinesh" and "php" to "java" and "is" as "was".
Please tell me any simple method to do this......It is very urgent.......
Anybody help me.........Advance thanks for all........plz help me......
Oct 24 '07 #1
Share this Question
Share on Google+
1 Reply

realin
100+
P: 254
what u can do is open the file in write mode.. using the inbuilt function fopen() it will place ur pointer to the start of the file.. now read file line by line.. use preg_replace() to change the text and store back to the file.
or grab the contents in a varible, change it using preg_replace() again and write it back to the file..
Oct 24 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.