View Single Post
Old 30-03-2005, 09:26   #1 (permalink)
mattih5
Registered User
 
mattih5's Avatar
 
Join Date: Mar 2005
Location: Manchester
Posts: 62
PHP - Finding the occurences of a string in a file

Hi all,

I have this script which I have been developing @ university, it is supposed to search a for a directory or a file within a directory - the front end form hasn't been designed yet.

Anyway, i'm concentrating on searching for a string within a file @ the moment as I have a deadline for 2 weeks. Within the While loop in the Else portion of the IF statement is supposed to find all occurences of the string assigned to $search, which could be anything - ultimately, but for test purposes I have assigned 'Test' as the string.

Up to yet I have the script working so that it checks all files in a directory for this string and returns the path of the file. When the script has printed the file path, I have it so that it prints the number of lines the search string was found on.

But, I need to get the script to function so that it does exactly above, but, alongside printing the line numbers the search term was found on (starting @ 1 not 0), it has to print the whole line of text with the search word in bold and uppercase.

Please could anyone provide me with some pointers as i'm tearing my hear out over it. I kind of understand how to go about it, but I cannot turn this logic into code.

Below is the code I have so far:

Quote:
[INDENT]<?php

//A recursive function: Displays content of directory passed as argument + all its sudirectories. //Displays directory names. Displays files as Hot URLs. Indents to show levels of nesting.
function display_directory($dir1)
{
STATIC $min_depth = 20;//initialised to greater than any likely depth

if(!is_string($dir1))
return FALSE;

//Calculate indentation for display:
//NB DOS slash used to split string into an array:
$depth = explode("\\", $dir1); //Make directory string into an array
$current_depth = sizeof($depth);//Count no. of elements in array
//First time through display_directory(), $min_depth will be set
//(Every other time, $current_depth will be >= min_depth)
if($current_depth < $min_depth)
$min_depth = $current_depth;

$dir = $dir1;
$dh = opendir($dir);
while ($file = readdir($dh))
{
//Don't print '.' & '..' directory entries:
if(($file != ".") && ($file != ".."))
{
$file = $dir."\\".$file; //NB DOS slash to join path & file names
//Do indentation:
for($i=0;$i <= ($current_depth - $min_depth); $i++)
//NB @ suppresses error messages: only way to test for DOS file v. directory
if($ddh = @opendir($file)) //NB @ means ‘suppress error messages’
{
//then $file is a directory
print "<b>Directory: $file</b><br>";
display_directory($file); //NB function calls itself recursively
}
else
{
$search = "test";
$srcfile = $file;
$TestString = @implode('', file($srcfile));
$nmatches = preg_match_all("/($search)/", $TestString, $matches);
if ($nmatches != '0') {
print "<a href=\"".$file."\">".$file."</a><br/>";
while(list($key,$val) = each($matches[1])){
print "<strong>Line</strong> "."<b>$key; </b><br>";
print "&nbsp;&nbsp;";
print "<strong>".strtoupper($val)."</strong>"."<br/>";
}
}

}
}//end if (!. && !..)
}//end while
closedir($dh);
return TRUE;
}//end of function

//Script START:
print "<b>Search string found in ".getcwd().":</b><br><br>";
display_directory(getcwd()); //Lists current working directory


?>
[/indent]
  Reply With Quote