PHP Classes

File: stringsearcher.php

Recommend this page to a friend!
  Classes of Bao Nguyen Quoc   String Searcher   stringsearcher.php   Download  
File: stringsearcher.php
Role: Class source
Content type: text/plain
Description: Class
Class: String Searcher
Search for text in files of any size
Author: By
Last change: Accessible without user login
Date: 18 years ago
Size: 2,624 bytes
 

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;
    }
}


?>