note 91857 added to function.strpos
| From: | dpharris at llu dot edu | Date: | Mon, 29 Jun 2009 15:54:19 +0000 |
| Subject: | note 91857 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157671@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 64.71.164.2
Probable Submitter: 151.112.12.63
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/91857
Del: integrated -- https://master.php.net/note/delete/91857/integrated
Del: useless -- https://master.php.net/note/delete/91857/useless
Del: bad code -- https://master.php.net/note/delete/91857/bad+code
Del: spam -- https://master.php.net/note/delete/91857/spam
Del: non-english -- https://master.php.net/note/delete/91857/non-english
Del: in docs -- https://master.php.net/note/delete/91857/in+docs
Del: other reasons-- https://master.php.net/note/delete/91857
Reject -- https://master.php.net/note/reject/91857
Search -- https://master.php.net/manage/user-notes.php