note 92516 deleted from function.strpos by danbrown

From: Date: Fri, 13 Aug 2010 13:31:40 +0000
Subject: note 92516 deleted from function.strpos by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171641@lists.php.net to get a copy of this message
Note Submitter: nathan at bitmesh dot com ---- 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 ?>

« previous php.notes (#171641) next »