axelheyst Posted January 31, 2007 Share Posted January 31, 2007 Is there a way to use a search function (stripos) such that:$num=stripos($haystack,$needle);where $needle is an array like:$needle=array('red','blue','green');Basically, I would like to know if $haystack contains any of these items. It would also be nice to know which item is contained in $haystack, and the start position of this item $needle[0] or $needle[1].Any thoughts? Thank you all! Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 [code]foreach($haystack AS $hay){ foreach($needle AS $ow){ if($stripos($hay, $ow) !== FALSE){ print $hay.' contains: '.$ow.' at '.$stripos($hay, $ow).'<br />; } }}[/code](not tested)Edit: is $haystack an array also? If it's not, just take out the first foreach, change to:[code] foreach($needle AS $ow){ if($stripos($haystack, $ow) !== FALSE){ print $haystack.' contains: '.$ow.' at '.$stripos($haystack, $ow).'<br />; } }[/code] Quote Link to comment Share on other sites More sharing options...
KrisNz Posted January 31, 2007 Share Posted January 31, 2007 If I'm reading this right it sounds like you want[url=http://au3.php.net/manual/en/function.in-array.php]http://au3.php.net/manual/en/function.in-array.php[/url] - returns bool or[url=http://au3.php.net/manual/en/function.array-search.php]http://au3.php.net/manual/en/function.array-search.php[/url] - returns key or false if not found Quote Link to comment Share on other sites More sharing options...
axelheyst Posted January 31, 2007 Author Share Posted January 31, 2007 @jesirose: That worked like a charm. thank you very much!@KrisNz: thanks also, not quite what I needed. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 [s]Which one? I'm curious to know if I understood what you were trying to do. Was it the in_array, or what I wrote?[/s]Edit: Great! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.