Location: PHPKode > scripts > LineRemover > lineremover/class.LineRemover.inc
<?php

/*
    LineRemover ver 1.0.0
    Author: Daniel Kushner
    Email: hide@address.com
    Release: 2 Nov 2001
    Copyright 2001
    www.websapp.com/classes
    
    Removes lines containing a given regular expression from the given files.
    
*/
class LineRemover {
    
    var $files = array();
    var $ereg = '';
    var $ignoreCase = false;
    
    function LineRemover($files = '') {
        $this->addFiles($files);
    }
    
    function addEreg($ereg) {
        $this->ereg = $ereg;
        $this->ignoreCase = false;
    }
    
    function addEregi($eregi) {
        $this->ereg = $eregi;
        $this->ignoreCase = true;
    }
    
    function addFile($file) {
        if($file != '') {
            $this->files[] = $file; 
        }
    }
    
    function addFiles($files) {
        if(is_array($files)) {
            foreach($files as $file) {
                $this->addFile($file);
            }
        }
    }
    
    
    function remove() {
        
        if(($this->ereg != '') && (count($this->files) > 0)) {
            
            // Before removing line a check that each file can be open and read is done
            foreach($this->files as $file) {
                $fp = @fopen($file, 'r') 
                    or die("Can not open the file $file for reading. Job terminated");
                fclose($fp);
            }
            
            $counter = 0;
            
            foreach($this->files as $file) {
                $fp = @fopen($file, 'r');
                $arr = array();
                while (!feof($fp)) {
                    $line = fgets($fp,4096);
                    if($this->ignoreCase) {
                        if(!eregi($this->ereg, $line)) {
                            $arr[] = $line;
                        } elseif(!ereg($this->ereg, $line)) {
                            $arr[] = $line;
                        } else {
                            $counter++;
                        }
                    }
                }
                fclose($fp);
                $fp = @fopen($file, 'w')
                    or die("Can not open the file $file for writting. Job terminated");
                foreach($arr as $line) {
                    fputs($fp, $line);
                }
                fclose($fp);
            }
            return $counter;
        } else {
            return 0;
        }
    }
}


?>
Return current item: LineRemover