PHP Classes
Icontem

File: stringsearcher.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Bao Nguyen Quoc  >  String Searcher  >  stringsearcher.php  
File: stringsearcher.php
Role: Class source
Content type: text/plain
Description: Class
Class: String Searcher
Search for text in files of any size
 

Contents

Class file image Download
<?

class StringSearcher
{
    
/**
     * Seek position
     *
     * @var int
     */
    
var $seek 0;
    
    
/**
     * File pointer
     * 
     * @var resource
     */
    
var $pointer null;
    
    
/**
     * Search string
     *
     * @var string
     */
    
var $search "";
    
/**
     * Buffer
     * 
     * @var string
     */
    
var $buffer "";
    
    
/**
     * Found seek
     * 
     * @var int
     */
    
var $found = -1;
    
    
/**
     * Matchcase
     * @var bool
     */
    
var $matchcase true;
    
    
/**
     * Read next buffer
     *
     * @return string
     */
    
function read()
    {
        
$buffer "";
        if (
$this->pointer && !feof($this->pointer))
        {    
            
fseek($this->pointer,$this->seek);
            
            
$readlength 20480//20 kb
            
            //enough to contain search
            
if (($readlength strlen($this->buffer)) < strlen($this->search))
            {
                
$readlength strlen($this->search) - strlen($this->buffer) + $readlength;
            }
            
            
$buffer fread($this->pointer,$readlength);
        }
        return 
$buffer;
    }
    
    
/**
     * Search for next string
     *
     * @return bool
     */
    
function search()
    {
        if (!
$this->pointer//where do you want me to find ?
        
{
            return 
false;
        }
        if (
$this->search == ""//what do you want me to find ?
        
{
            return 
false;
        }
        
        
$buffer =& $this->buffer//back reference
        
$seek =& $this->seek;
        
        if (
strlen($buffer) <= strlen($this->search)) //update buffer
        
{
            
$buffer $this->read();
            
            if (
strlen($buffer) < strlen($this->search)) //no more to find :(
            
{
                return 
false;
            }
            
            if (!
$this->matchcase)
            {
                
$buffer strtolower($buffer);
                
$this->search strtolower($this->search);
            }
            
        }
        
        
//search in buffer
        
$pos strpos($buffer,$this->search);
        if (
$pos !== false//got it :)
        
{
            
$seek += $pos 1;
            
            
$buffer substr($buffer,$pos 1);
            
$this->found $seek-1;
        }
        else 
//nope :(
        
{
            if (
strlen($this->search)  > 1)
            {
                
$seek += strlen($buffer) - strlen($this->search) + 1;
                
$buffer substr($buffer,-strlen($this->search) + 1);
            }
            else
            {
                
$seek += strlen($buffer);
                
$buffer "";
            }
            
            
            
$this->found = -1;
        }
        return 
true;
    }
    
    
/**
     * Open file for searching
     *
     * @return bool
     */
    
function open($filename)
    {
        
$this->close();
        
//open file and reset seek
        
$this->pointer = @fopen($filename,"rb");
        
$this->seek 0;
        
$this->found = -1;
        return (
$this->pointer != null);
    }
    
    
/**
     * Close file and stop searching
     *
     */
    
function close()
    {
        if (
$this->pointer) @fclose($this->pointer);
        
$this->pointer null;
    }
}


?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products