note 99197 deleted from function.strpos by danbrown
| From: | danbrown@php.net | Date: | Mon, 02 Aug 2010 18:40:41 +0000 |
| Subject: | note 99197 deleted from function.strpos by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171339@lists.php.net to get a copy of this message | ||
Note Submitter: php at pommepause dot com
----
Here's my own implementation of a strpos function that allows wildcards in the needle.
If the $needle starts with a * and is found to match the $haystack, 0 will be returned.
<?php
function gh_wild_strpos($haystack, $needle, $offset = 0) {
$is_wild = (strpos($needle, "*") !== FALSE);
if (!$is_wild) {
return strpos($haystack, $needle, $offset);
}
$haystack = substr($haystack, $offset);
$needles = explode("*", $needle);
if ($needle[0] == '*') {
$first_index = 0;
}
foreach ($needles as $needle_part) {
if ($needle_part == '') {
continue;
}
$needle_index = strpos($haystack, $needle_part);
if (!isset($first_index)) {
$first_index = $needle_index;
}
if ($needle_index === FALSE) {
return FALSE;
} else {
$found = TRUE;
$haystack = substr($haystack, $needle_index + strlen($needle_part));
}
}
if ($found) {
return $first_index;
}
return FALSE;
}
?>