Login   Register  
PHP Classes
elePHPant
Icontem

File: class.LineRemover.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Daniel Kushner  >  LineRemover  >  class.LineRemover.inc  >  Download  
File: class.LineRemover.inc
Role: ???
Content type: text/plain
Description: Removes lines containing a given regular expression from the given files.
Class: LineRemover
Author: By
Last change:
Date: 2001-11-02 22:58
Size: 2,489 bytes
 

Contents

Class file image Download
<?php

/*
    LineRemover ver 1.0.0
    Author: Daniel Kushner
    Email: daniel@websapp.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;
        }
    }
}


?>