note 107351 added to function.strpos

From: Date: Thu, 02 Feb 2012 14:31:08 +0000
Subject: note 107351 added to function.strpos
Groups: php.notes 
Request: Send a blank email to php-notes+get-185830@lists.php.net to get a copy of this message
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. ---- Server IP: 69.147.83.197 Probable Submitter: 66.128.95.242 ---- Manual Page -- http://www.php.net/manual/en/function.strpos.php Edit -- https://master.php.net/note/edit/107351 Del: integrated -- https://master.php.net/note/delete/107351/integrated Del: useless -- https://master.php.net/note/delete/107351/useless Del: bad code -- https://master.php.net/note/delete/107351/bad+code Del: spam -- https://master.php.net/note/delete/107351/spam Del: non-english -- https://master.php.net/note/delete/107351/non-english Del: in docs -- https://master.php.net/note/delete/107351/in+docs Del: other reasons-- https://master.php.net/note/delete/107351 Reject -- https://master.php.net/note/reject/107351 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185830) next »