PHP Classes

PHP Search Large Files: Search large files that would not fit in memory

Recommend this page to a friend!
  Info   View files Example   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 54%Total: 450 All time: 6,163 This week: 404Up
Version License PHP version Categories
searchable-file 1.1.4BSD License5.5PHP 5, Files and Folders, Searching, T...
Description 

Author

This class can search large files that would not fit in memory.

It provides several class functions with similar names to those that PHP provides for searching strings but it accesses the contents of large files without actually loading all at once in memory.

Currently it provides functions for searching case sensitive and insensitive text like strpos and stripos, search for multiple strings, get the contents of a section of a file like substr, and perform regular expression matching like pcre_match and pcre_match_all.

Innovation Award
PHP Programming Innovation award nominee
April 2016
Number 10
PHP provides a great set of functions for manipulating and searching text. However the text to be searched needs to be loaded in memory as a string.

If you need to search text in large files, the regular PHP string search functions will not do, because it may exceed the configured PHP memory limits to load the file in a single string.

This PHP class provides clever solutions to overcome the limitations of PHP string search functions so it can work on text from large files. It can even perform regular expression matching on large text files.

It provides functions with the same names of the regular PHP functions for searching text in string variables.

Manuel Lemos
Picture of Christian Vigh
  Performance   Level  
Name: Christian Vigh <contact>
Classes: 32 packages by
Country: France France
Age: 57
All time rank: 13810 in France France
Week rank: 10 Up1 in France France Up
Innovation award
Innovation award
Nominee: 20x

Winner: 3x

Example

<?php
   
include ( '../SearchableFile.phpclass' ) ;
   
$file = 'verybigfile.rtf' ;
   
$t1 = microtime ( true ) ;

   
$sf = new SearchableFile ( ) ;
   
$sf -> Open ( $file ) ;
   
$pos = 0 ;
   
$search = '\\pict' ;
   
$length = strlen ( $search ) ;
   
$pos1 = [] ;
   
$pos2 = [] ;

    while ( (
$pos = $sf -> strpos ( $search, $pos ) ) !== false )
    {
       
//echo "POS1 = $pos\n" ;
       
$pos += $length ;
       
$pos1 [] = $pos ;
    }

   
$t2 = microtime ( true ) ;
   
$contents = file_get_contents ( $file ) ;
   
$pos = 0 ;

    while ( (
$pos = strpos ( $contents, $search, $pos ) ) !== false )
    {
       
//echo "POS2 = $pos\n" ;
       
$pos += $length ;
       
$pos2 [] = $pos ;
    }
   
$t3 = microtime ( true ) ;

    echo
"Using SearchableFile : " . round ( $t2 - $t1, 3 ) . "\n" ;
    echo
"Using file_get_contents : " . round ( $t3 - $t2, 3 ) . "\n" ;

    if (
count ( $pos1 ) != count ( $pos2 ) )
        echo
"Result count mismatch : " . count ( $pos1 ) . " (SearchableFile), " . count ( $pos2 ) . " (file_get_contents)\n" ;
    else
    {
        for (
$i = 0 ; $i < count ( $pos1 ) ; $i ++ )
        {
            if (
$pos1 [$i] != $pos2 [$i] )
                echo
"Result mismatch at index #$i : {$pos1 [$i]} (SearchableFile), {$pos2 [$i]} (file_get_contents)" ;
        }
    }


  Files folder image Files  
File Role Description
Files folder imageexamples (4 files)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file NOTICE Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Plain text file SearchableFile.phpclass Class Class source

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file multistrpos.php Example Example script
  Accessible without login Plain text file preg_match.php Example Example script
  Accessible without login Plain text file preg_match_all.php Example Example script
  Accessible without login Plain text file strpos.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:450
This week:0
All time:6,163
This week:404Up
 User Ratings  
 
 All time
Utility:66%StarStarStarStar
Consistency:66%StarStarStarStar
Documentation:75%StarStarStarStar
Examples:66%StarStarStarStar
Tests:-
Videos:-
Overall:54%StarStarStar
Rank:2013