Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing file content in PHP

Tags:

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

like image 641
Emanuil Rusev Avatar asked Aug 26 '10 12:08

Emanuil Rusev


People also ask

How to replace content of a file in php?

$f1 = fopen('mysourcefile', 'r'); $f2 = fopen('mytmpfile', 'w'); $search = array('foo', 'bar'); $replace = array('bar', 'baz'); $name = 'replacetext. '. implode(',', $search).

How to change file in php?

The rename() function in PHP is an inbuilt function which is used to rename a file or directory.

What is file_ put_ contents in php?

The file_put_contents() writes data to a file. This function follows these rules when accessing a file: If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of filename. Create the file if it does not exist.

How do I replace a word in a string in PHP?

Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.


2 Answers

You can do:

$file = 'filename'; file_put_contents($file,str_replace('find','replace',file_get_contents($file))); 
like image 130
codaddict Avatar answered Sep 22 '22 07:09

codaddict


@codaddict's answer is quite sufficent for small files (and would be how I would implement it if the size of the file was under a MiB). However it will eat up a ton of memory, and as such you should be careful when reading large files.

If you want a much more memory friendly version, you could use stream filters...

class ReplaceText_filter extends php_user_filter {     protected $search = '';     protected $replace = '';      public function filter($in, $out, &$consumed, $closing) {         while ($bucket = stream_bucket_make_writable($in)) {             $bucket->data = str_replace(                 $this->search,                  $this->replace,                  $bucket->data             );             $consumed += $bucket->datalen;             stream_bucket_append($out, $bucket);         }         return PSFS_PASS_ON;     }      public function onCreate() {         if (strpos($this->filtername, '.') === false) return false;         list ($name, $arguments) = explode('.', $this->filtername, 2);         $replace = '';         $search = $arguments;         if (strpos($arguments, '|') !== false) {             list ($search, $replace) = explode('|', $arguments, 2);         }         if (strpos($search, ',') !== false) {             $search = explode(',', $search);         }         if (strpos($replace, ',') !== false) {             $search = explode(',', $replace);         }         $this->search = $search;         $this->replace = $replace;     } } stream_filter_register('replacetext.*', 'ReplaceText_Filter'); 

So, then you can append an arbitrary stream filter. The filter's name determines the arguments:

$search = 'foo'; $replace = 'bar'; $name = 'replacetext.'.$search.'|'.$replace; stream_filter_append($stream, $name); 

or for arrays,

$search = array('foo', 'bar'); $replace = array('bar', 'baz'); $name = 'replacetext.'.implode(',', $search).'|'.implode(',', $replace); stream_filter_append($stream, $name); 

Obviously this is a really simple example (and doesn't do a lot of error checking), but it allows you to do something like this:

$f1 = fopen('mysourcefile', 'r'); $f2 = fopen('mytmpfile', 'w'); $search = array('foo', 'bar'); $replace = array('bar', 'baz'); $name = 'replacetext.'.implode(',', $search).'|'.implode(',', $replace); stream_filter_append($f1, $name); stream_copy_to_stream($f1, $f2); fclose($f1); fclose($f2); rename('mytmpfile', 'mysourcefile'); 

And that will keep memory usage very low while processing potentially huge (GiB or TiB) files...

Oh, and the other cool thing, is it can inline edit differing stream types. What I mean by that is that you can read from a HTTP stream, edit inline, and write to a file stream. It's quite powerful (as you can chain these filters)...

like image 22
ircmaxell Avatar answered Sep 20 '22 07:09

ircmaxell