note 27870 deleted from function.glob by sobak

From: Date: Thu, 01 Dec 2016 06:30:59 +0000
Subject: note 27870 deleted from function.glob by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207799@lists.php.net to get a copy of this message
Note Submitter: tmm at aon dot at ---- I have written my own function for searching files, but it only supports ? and * However it should be easily expandable. <?php // e.g. $matches=GetMachingFiles(GetContents("."),"*.txt"); function GetMatchingFiles($files, $search) { // Split to name and filetype if(strpos($search,".")) { $baseexp=substr($search,0,strpos($search,".")); $typeexp=substr($search,strpos($search,".")+1,strlen($search)); } else { $baseexp=$search; $typeexp=""; } // Escape all regexp Characters $baseexp=preg_quote($baseexp); $typeexp=preg_quote($typeexp); // Allow ? and * $baseexp=str_replace(array("\*","\?"), array(".*","."), $baseexp); $typeexp=str_replace(array("\*","\?"), array(".*","."), $typeexp); // Search for Matches $i=0; foreach($files as $file) { $filename=basename($file); if(strpos($filename,".")) { $base=substr($filename,0,strpos($filename,".")); $type=substr($filename,strpos($filename,".")+1,strlen($filename)); } else { $base=$filename; $type=""; } if(preg_match("/^".$baseexp."$/i",$base) && preg_match("/^".$typeexp."$/i",$type)) { $matches[$i]=$file; $i++; } } return $matches; } And if someone's searching for a function which gets all files from a directory including the subdirectories: // Returns all Files contained in given dir, including subdirs function GetContents($dir,$files=array()) { if(!($res=opendir($dir))) exit("$dir doesn't exist!"); while(($file=readdir($res))==TRUE) if($file!="." && $file!="..") if(is_dir("$dir/$file")) $files=GetContents("$dir/$file",$files); else array_push($files,"$dir/$file"); closedir($res); return $files; } ?>

« previous php.notes (#207799) next »