note 85501 added to function.in-array
| From: | alishahnovin at hotmail dot com | Date: | Tue, 02 Sep 2008 19:43:16 +0000 |
| Subject: | note 85501 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144090@lists.php.net to get a copy of this message | ||
Here's a function that does an in_array, but takes wildcards in the needle, and also can be
case sensitive/insensitive...
A few points:
-It doesn't use foreach, but for, which is quicker
-I didn't use regex to search with a wildcard for the reason that the needle could be
unpredictable if it's user-input, and rather than having to escape metacharacters, I decided it
would be easier to do a plain text comparison.
-Needles with wildcards can come in many forms such as:
Str*ng
S*r*ng*
*rng
*i*n*
so a split is being done on that string, and each part is then compared with the current item. If
the first part is not found, the comparison is done, and we move on. If it IS found, we move on to
the next part of the needle, while chopping off the initial part of the haystack string. This is to
ensure that each comparison of a needle part is looking at the next part of the haystack string.
For example:
needle: "Bo*bo"
haystack[0] = "Bob is lazy"
On the first pass, when searching "Bo", we then modify the haystack[0] to be: "b is
lazy" so that "bo" is compared with that. Otherwise, we'd be comparing
"bo" with "Bob is lazy", and returning true incorrectly.
I haven't fully tested the function, so let me know if you spot any bugs, or have any
questions.
<?php
function in_wildarray($needle, $haystack, $case_sensitive=true) {
$is_wild = (strpos($needle,"*")===true)? true : false;
$needles = ($is_wild)? explode("*", $needle) : array();
$needle = ($case_sensitive)? $needle : strtolower($needle);
for($i=0;$i<count($haystack);$i++) {
$haystack_str = ($case_sensitive)? haystack[$i] : strtolower($haystack[$i]);
if ($is_wild) {
$found = false;
for($x=0;$x<count($needles);$x++) {
$needle_part = trim($needles[x]);
$needle_index = strpos($haystack_str, $needle_part);
if ($needle_index===false) {
$found = false;
break; //break out of the loop, because string part is not found in the haystack
string
} else {
$found = true;
//chop off the start of the string to the needle_index
//so we can be sure that the found items are in the correct order
//and we are avoiding the potential of finding duplicate characters
$haystack_str = substr($haystack_str, 0, $needle_index);
}
}
if ($found) { return true; }
} elseif (!$is_wild && $haystack_str == $needle) {
return true;
}
}
return false;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 131.107.0.74
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/85501
Del: integrated -- https://master.php.net/note/delete/85501/integrated
Del: useless -- https://master.php.net/note/delete/85501/useless
Del: bad code -- https://master.php.net/note/delete/85501/bad+code
Del: spam -- https://master.php.net/note/delete/85501/spam
Del: non-english -- https://master.php.net/note/delete/85501/non-english
Del: in docs -- https://master.php.net/note/delete/85501/in+docs
Del: other reasons-- https://master.php.net/note/delete/85501
Reject -- https://master.php.net/note/reject/85501
Search -- https://master.php.net/manage/user-notes.php