note 92516 added to function.strpos
| From: | nathan at bitmesh dot com | Date: | Tue, 28 Jul 2009 04:06:25 +0000 |
| Subject: | note 92516 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158806@lists.php.net to get a copy of this message | ||
dpharris' script doesn't work if the needle is the first character in the array. It should
use !== false in case the string's position is 0. Here's my fix:
<?php
function strposa($haystack ,$needles=array(),$offset=0){
$chr = array();
foreach($needles as $needle){
if (strpos($haystack,$needle,$offset) !== false) {
$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: 208.69.120.35
Probable Submitter: 98.215.112.110
----
Manual Page -- http://www.php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/92516
Del: integrated -- https://master.php.net/note/delete/92516/integrated
Del: useless -- https://master.php.net/note/delete/92516/useless
Del: bad code -- https://master.php.net/note/delete/92516/bad+code
Del: spam -- https://master.php.net/note/delete/92516/spam
Del: non-english -- https://master.php.net/note/delete/92516/non-english
Del: in docs -- https://master.php.net/note/delete/92516/in+docs
Del: other reasons-- https://master.php.net/note/delete/92516
Reject -- https://master.php.net/note/reject/92516
Search -- https://master.php.net/manage/user-notes.php