Finding and Replacing Text Within a File in PHP


Read, replace, and save back to the file.

Source Code

$fileContents = file_get_contents("example.txt");
$fileContents = str_replace("oldText", "newText", $fileContents);
file_put_contents("example.txt", $fileContents);
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments