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! Link to comment https://forums.phpfreaks.com/topic/36444-solved-search-a-string-for-an-object-within-an-array/ 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] Link to comment https://forums.phpfreaks.com/topic/36444-solved-search-a-string-for-an-object-within-an-array/#findComment-173423 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 Link to comment https://forums.phpfreaks.com/topic/36444-solved-search-a-string-for-an-object-within-an-array/#findComment-173427 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. Link to comment https://forums.phpfreaks.com/topic/36444-solved-search-a-string-for-an-object-within-an-array/#findComment-173430 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! Link to comment https://forums.phpfreaks.com/topic/36444-solved-search-a-string-for-an-object-within-an-array/#findComment-173435 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.