/home/drscream

  • home
  • blog
  • gallery
  • about
  • 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";
    }
    

    Posted

    July 31, 2007

    Tags

    php, search

    2 comments