note 91857 deleted from function.strpos by danbrown
| From: | danbrown@php.net | Date: | Fri, 13 Aug 2010 13:32:29 +0000 |
| Subject: | note 91857 deleted from function.strpos by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171642@lists.php.net to get a copy of this message | ||
Note Submitter: dpharris at llu dot edu
----
vbracco wonderful function below (04-Apr-2009 02:45) doesn't check for if the result is null.
My use of the function returned an empty set as the result of min. Here is my correction.
vbracco, thanks for saving me some time.
<?php
function strposa($haystack ,$needles=array(),$offset=0){
$chr = array();
foreach($needles as $needle){
if (strpos($haystack,$needle,$offset)) {
$chr[] = strpos($haystack,$needle,$offset);
}
}
if(empty($chr)) return false;
return min($chr);
}
$string = "This is my string, very simple.";
echo strposa($string,array(".",","," ")); // 2
echo strposa($string,array("T")); // 0
echo strposa($string,array("Q","W")); // false
?>