note 27870 modified in function.glob by aidan

From: Date: Thu, 09 Dec 2004 03:44:54 +0000
Subject: note 27870 modified in function.glob by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-81565@lists.php.net to get a copy of this message
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; } ?> --was-- I have written my own function for searching files, but it only supports ? and * However it should be easily expandable. // 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; } http://php.net/manual/en/function.glob.php

« previous php.notes (#81565) next »