note 37441 deleted from function.array-search by didou

From: Date: Tue, 16 Dec 2003 09:36:43 +0000
Subject: note 37441 deleted from function.array-search by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-62036@lists.php.net to get a copy of this message
Note Submitter: name@phism.org ---- here's a way to do a case insensitive array search that accepts wildcards and disregards file extensions 4 letters or less: function searchar($string, $array) { $ao = 0; $ap = 0; $arr = array(); $string = strtolower($string); $posval = strpos($string, '*'); $arr4 = explode('*', $string); while($ao<= (count($array) - 1)){ $go = 0; $array[$ao] = strtolower($array[$ao]); if(strpos($array[$ao], '.')!== false){ if(strlen($array[$ao]>= 5)){ $setv = 5; } else{ $setv = strlen($array[$ao]); } $strr2 = substr($array[$ao], strpos($array[$ao], '.', (strlen($array[$ao]) - $setv))); $array[$ao] = substr($array[$ao], 0, (strlen($array[$ao]) - strlen($strr2))); } if($posval!== false && $string!== '*'){ $as = 0; $doit = 1; while($as<= (count($arr4) - 1)){ if($arr4[$as]!== NULL && $arr4[$as]!== ''){ $arr5[$as] = substr($array[$ao], strpos($array[$ao], $arr4[$as]), strlen($arr4[$as])); if($as== 0){ $arr6[$as] = strpos($array[$ao], $arr4[$as]); } else{ $offset = ($arr6[$as - 1] + strlen($arr5[$as - 1])); $arr6[$as] = strpos($array[$ao], $arr4[$as], $offset); } } $as++; } if(join($arr4)!== join($arr5)){ $doit = 0; } if(count($arr4)> 2){ $at = 0; while($at<= (count($arr5) - 1)){ if($at!== 0 && $arr6[$at]< $arr6[$at - 1]){ $doit = 0; } $at++; } } if($doit== 1 && $posval> 0 && $arr4[0]!== substr($array[$ao], 0, strlen($arr4[0]))){ $doit = 0; } if($doit== 1 && false== strpos($string, '*', (strlen($string) - 1))){ if($arr4[count($arr4) - 1]!== substr($array[$ao], (strlen($array[$ao]) - strlen($arr4[count($arr4) - 1])))){ $doit = 0; } } if($doit== 1){ $go = 1; } } if($string== $array[$ao]){ $go = 1; } if($string== implode(" ", explode("_", $array[$ao]))){ $go = 1; } if($go== 1 && !in_array($ao, $arr)){ $arr[$ap] = $ao; $ap++; } $ao++; } return $arr; } it may not be efficient, but it does the job.

« previous php.notes (#62036) next »