Home / PHP Tips Generated By ChatGPT / 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);