Search and Replace a Loaded File (with PHP)

A simple to use script for loading a file, search and replacing words, and displaying the file.


// File to open
$file = '/path/to/file.txt';
// Search and Replace Arrays
$search = array('soda', 'hamburger', 'ice', 'food');
$replace = array('beer', 'pizza', 'water', 'drink');
// Open the file
$lines = file($file);
// Read through the file
foreach($lines as $line_num => $line) {
    $text = preg_replace($search, $replace, $line);
    echo $text."\n";
}