Can any body tell me how to replace some text after searching in file and if there is method it should not disturb the file text formate,mean every text line should be as it was before. Should not in one line text.
Thank you very much in advance
How to replace some text in a file after searching
Moderator: General Moderators
Re: How to replace some text in a file after searching
I think I know what you're looking for - let me know if I've misunderstood. As a simple example, suppose you want to replace all instances of the word "dog" with "cat":
First, file_get_contents() opens the specified file, and reads the whole thing into a string, $contents. str_replace() (the documentation is here) will replace every occurrence of the first string - in this case "dog" - with the second - "cat". str_ireplace() works exactly the same, but is case insensitive. You can then use file_put_contents() to save the file.
Code: Select all
$filePath = "myfile.txt";
$contents = file_get_contents($filePath);
$contents = str_replace("dog", "cat", $contents);
file_put_contents($filePath, $contents);
-
jegan.aaodis
- Forum Newbie
- Posts: 15
- Joined: Fri Oct 09, 2009 1:56 am
Re: How to replace some text in a file after searching
Thanks that was a good .
It helps me.
It helps me.