Jump to content

PHP File text search


houssam_ballout

Recommended Posts

perhaps something like this?

<?PHP
/* seach all the txt files in a directory for a string */
/* $files = glob("/path/to/directory/*.txt"); */
$files = glob("ajaxfiles/*.htm");

/* initialize a counter */
$start = 0;

/* count the number of files found */
$end = count($files);

/* set the value for whatever it is you are seaching for  */
$needle = "speed";

/* start looping thru the files */
while($start<$end) {
/* get the contents of the file */
$filecontents = file_get_contents($files[$start]);
/* check to see if the file contains your search value */
/* if yes, store the file name into a new array */
if(!stristr($filecontents, $needle) === FALSE) {
	$goodfiles[] = $files[$start];
}
/* increment your counter */
$start ++;
}
/* display the list of files that contain your search value */
print_r($goodfiles);

?>

Link to comment
Share on other sites

Directory called test-search

and the file I am using for this is called: test2.php

 



<?PHP
/* seach all the txt files in a directory for a string */
/* $files = glob("/path/to/directory/*.txt"); */
$files = glob("test-search/*.html");


/* initialize a counter */
$start = 0;

/* count the number of files found */
$end = count($files);

/* set the value for whatever it is you are seaching for  */
$needle = "test";

/* start looping thru the files */
while($start<$end) {



/* get the contents of the file */



$filecontents = file_get_contents($files[$start]);



/* check to see if the file contains your search value */



/* if yes, store the file name into a new array */



if(!stristr($filecontents, $needle) === FALSE) {





$goodfiles[] = $files[$start];



}



/* increment your counter */



$start ++;

}

/* display the list of files that contain your search value */
print_r($goodfiles);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.