PhpSnip.com

User Stats

search

This function was widely inspired by dwebb's File Search function (see: http://www.zend.com/codex.php?id=301&single=1) It's basically the same function but now it returns an array to support multiple matches, it's also more flexible because I used stristr() instead of a literal match, wich is quite useless for a search function...

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 448

Source Code ( 43 lines )

<?php 
/*----------------------------------------------------------[v2.0]--+
 |	function search($dir,$needle,$recurse=false)					|
 |																	|
 |	Inputs															|
 |	@param	str		dir		valid path to search in 				|
 |	@param	str		needle	keyword to match 						|
 |	@param	bol		recurse	recursive search (default false)		|
 |																	|
 |	Outputs															|
 |	- false:		no match										|
 |	- array:		list of matches									|
 |																	|
 |	Author: h3														|
 |	mail: 	h3@mindkind.org											|
 |	credits: widely inspired from dwebb's File Search function		|
 |			(see: http://www.zend.com/codex.php?id=301&single=1)	|
 |																	|
 +------------------------------------------------------------------*/
function search($dir,$needle,$recurse=false) {
	if(is_dir($dir)) {
		$handle = @opendir($dir);
		$o		= array();
		while($file=readdir($handle)) {
			if (($file=='.')||($file=='..')) continue;
       		if(stristr(strtolower($file),strtolower($needle))) {
       			$o[] = $dir.$file;
       		}
			if(is_dir($dir.'/'.$file)&&$recurse==true) {
				$subresults = search($dir.'/'.$file,$needle,true);
       			if(!empty($subresults)) {       	 
					$o = array_merge($o,$subresults);
				}
       		}
		}
		if(!empty($o)) {
			return $o;
		} else {
			return false;
		}
	}
}
?>

Search

Subscribe

  Rss Feeds

Sponsors

Advertise