logo

Search class

Add comment

I’ve made a class that will search files for a given word/words. All you have to do is set parent folder, query and you’ll get results.
Script uses objects and I plan to make it better and more features. For now it’s very simple.

Example of how to use my class.

$search = new searchFiles();
//We need to set some params
/Regular expression to determine which files will be searched (only those that match pattern)
$search -> setFilter ( '/.*\.php/i' );
//Search as whole word or split it into smaller chunks and search them individually
$search -> setIsWholeWord ( false );
//Start tag that will wrap result
$search -> setBegin ( '<span style="color: red">' );
//End tag that will wrap result
$search -> setEnd ( '</span>' );
//Is our query regular expression
$search -> setUseRegex ( false );
//Do we want to search subfolders?
$search -> setIsRecursive ( true ); 

//Or you can do it like this
/*
$search -> setFilter ( '/.*\.php/i' ) -> setIsWholeWord ( false ) -> setBegin ( '<span style="color: red">' )
-> setEnd ( '</span>' ) -> setUseRegex ( false ) -> setIsRecursive ( true );
*/
//Here we set our query and start folder (if we sat TRUE to recursive all sub folders will be searched)
$rez = $search -> search ( 'php', 'C:\folder' );
//Loop through each file that has match
foreach ( $rez as $k ) {
        //Echo file path
        echo '<strong>', $k -> getFileName(), '</strong>';
        //Loop through each row that has result
        foreach ($k as $v) {
            //Echo line number and line that has match (result is wrapped in start/end tag)
            echo $v -> getLine(), ':', $v -> getText(), '';
    }
} 

Later I’ll add more features and make it better. Please report any bug.

You can download it here.

Related Posts
  • 05.06.2009 — PHP Disk Space Explorer Class (0)
    In this tutorial I’ll show you how to create a script that will create graphs. Those graphs will con…
  • 02.09.2009 — PHP Walker Class (1)
    In this tutorial we will create walker class. This class will load results by executing given SQL qu…
  • 26.04.2009 — Adjustable visit counter (2)
    Here’s a simple tutorial on how to create a simple and adjustable visits counter. Our counter will u…
  • 19.04.2009 — Multi-query function (5)
    I needed a function that will take a string or a file and then split it into small SQL queries. I fo…
  • 13.04.2009 — 5 useful functions (1)
    I would like to share with you a couple of functions that use a lot and I consider them quite useful…
  • 02.01.2010 — Enable E-mail In PHP – Win (0)
    This will be a quick tutorial that will show you how to enable e-mail function in PHP on Windows….
  • 23.12.2009 — Learning Resources (0)
    Reader Satish requested a list of tutorials where he could learn about PHP, MySQL and jQuery. Since …

logo

1 comment to “Search class”

  1. klint says:

    ur good..

Leave a Reply


 *


 *


logo
logo
Powered by Wordpress | Designed by Elegant Themes | CopyRight ©2010 php4every1.com