note 107351 deleted from function.strpos by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 May 2012 21:24:54 +0000 |
| Subject: | note 107351 deleted from function.strpos by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187882@lists.php.net to get a copy of this message | ||
Note Submitter: Logan Baron
----
After looking at the strposa function posted earlier, I noticed that if one of the needles tested
false when being checked in its own strpos, the function would always return false. So, I re-wrote
it to return the lowest index of the first occurring match.
private function strposa($haystack, $needles=array(), $offset=0)
{
$chr = array();
foreach($needles as $needle)
{
$res = strpos($haystack, $needle, $offset);
if ($res !== false) $chr[$needle] = $res;
}
if(empty($chr)) return false;
return min($chr);
}
Now, it simply doesn't add that needle's strpos value if it's false. The check that
already existed will be useful here, if nothing got added to the array there were no matches, so
return false. However this time, it will return something other than 0 (false) if there was a match
but also a non-match in the array of needles.