Zend - The PHP Company




Miscellaneous

Add Code


MCSerch  

Type: application
Added by: gad106
Entered: 06/02/2002
Last modified: 02/12/2001
Rating: - (fewer than 3 votes)
Views: 4018
Searches through directories of text based files for a string. Configurable parameters are: extensions to search through, and directories to search through. I use it to search through my code library.


<html>
<head>
<title>MCSerch</title>
</head>
<body>

<?php

/***************************************************
*******            MCSerch                **********
*******                                   **********
*******       Created By: gad106          **********
*******       Assisted By: gad53          **********
*******    myunitishuge@hotmail.com       **********
*******                                   **********
*******        Use as you wish            **********
*******  Give credit where credit is due  **********
***************************************************/

/**
 *  Configuration Parameters
 */    
    
global $maxFind;    //maximum number of hits per document.
    
global $maxTotal;    //maximum number of hits returned.
    
global $maxMessage//error message to display when maximum total is hit.
    
global $dirs;        //directory array.
    
    
$maxFind 4;
    
$maxTotal 12;
    
$maxMessage "<b>Maximum Number of Hits Reached: </b>$maxTotal";
    
    
/**
     * dirs is an array of the directories to search through.
     */
    
$dirs = array();
    
$dirs[0] = 'C:/http';
    
$dirs[1] = 'C:/http/site';
        
$dirs[2] = '../';
    
    
/**
     * searchable is an array of allowable extensions to search in.
     */
    
$searchable = array();
    
$searchable[0] = 'php';
    
$searchable[1] = 'php3';
    
$searchable[2] = 'cgi';
    
/**
 * Do not change anything right below here:
 */
define(NL"n");

    
/**
     * Greeting
     */
    
echo 'MCSerch v.0.9<BR>'.NL;
    echo 
'Left column is the file, right column is the line number.<br>'.NL;
    echo 
'To configure, alter script (very easy)'.NL;

    
/*
     * the form with the search field
     */
    
echo '<form name="searchForm" method="get" action="'.$PHP_SELF.'">'.NL;
    echo 
'<input type="text" name="search_word"> <br>'.NL;
    echo 
'<input type="submit" value="search!"><br>'.NL;
    echo 
'</form>';
    
if (
$HTTP_GET_VARS['search_word']);      //make sure we've got something to search for
{
    echo 
'<table width="20%" bgcolor="#CCCCCC" border="1" bordercolor="#FFFFFF">'.NL;
    
    for (
$d=0$d<count($dirs); $d++)    //flip through each of the directory listings in $dirs
    
{
        
        if (
is_dir($dirs[$d]))            //make sure it actually is a directory
        
{
            echo 
'<tr><td bgcolor="#000000"><b><font color="#FFFFFF">Directory: '.$dirs[$d].'</font></b></td></tr>'.NL;
            
$dh opendir($dirs[$d]);    //create a directory handle
    
            
while ($fh readdir($dh))    //going through each file in the directory
            
{
                                        
//below: no directories allowed.
                
if ($fh != '.' && $fh != '..' && checkExtensions($fh$searchable) && !(is_dir($fh)))
                {
                    
$fn $dirs[$d].'/'.$fh;      //generate a usable filename 
                    
findHits($fn$HTTP_GET_VARS['search_word']);  //run the findHits routine
                
}
            }
        }
    }
    echo 
'</table>'.NL;
}

/**
 * checkFile makes sure that the file is listed in the $searchable array
 * if the extension fits, then return true
 */
function checkExtensions($fileHandle$extensions)
{
    
$isGood false;
    
    for (
$i=0$i<count($extensions); $i++)
    {
        if (
stristr($fileHandle$extensions[$i]))
        {
            
$isGood true;
        }
    }
    
    return 
$isGood;
}
    
/**
 * findHits opens the file and starts counting 2 things, line numbers and number of hits.
 * when a hit is found, the filename, and line number are reported. 
 */
function findHits($fh$wordToSearchFor)
{    
    global 
$maxFind;
    global 
$maxTotal;
    global 
$maxMessage;
        
    
$findCount 0;          //reset hits found

    
$fp fopen($fh'r');    //file pointer to our passed file
    
    
$lineNum 0;            //reset line numbers
    
        
while (!feof($fp))  //loop through each line
        
{
            
$lineNum++;        //count up line number
            
if ($fs fgets($fp2048))                //get the next line of data from the file
            
{
                if (
stristr($fs$wordToSearchFor))    //search the line for the search word
                
{
                    echo 
"<tr><td bgColor="$bg">".$fh.'</td><td>'.$lineNum.'</td></tr>'.NL;  //report the hit

                    
$findCount++;    //tick up hit counter.
                    
$maxTotal--;    //count down maximum total hits.

                    
if ($findCount>$maxFind)
                    {
                        break;
                    }
                    
                    if (
$maxTotal<=0)    //kill the script when we hit the total number of 
                    
{
                        die(
$maxMessage);
                    }
                }
            }
        }
}
?>

</body>
</html>


Usage Example


See the example


Rate This Script





Search



This Category All Categories