Replacing file content in PHP

Emanuil Rusev picture Emanuil Rusev · Aug 26, 2010 · Viewed 31.6k times · Source

I need a function just like preg_replace but instead of strings I need it to work with files / file content.

Answer

codaddict picture codaddict · Aug 26, 2010

You can do:

$file = 'filename';
file_put_contents($file,str_replace('find','replace',file_get_contents($file)));