gvp16 Posted January 26, 2011 Share Posted January 26, 2011 Hi, how can I match two keywords in a string? eg. if keywords (Kids,Hoody) are in this string (Animal Kids Basic Hoody Blue) do something..... Thanks. Link to comment https://forums.phpfreaks.com/topic/225734-match-2-words-in-string/ Share on other sites More sharing options...
Roman Fedorov Posted January 26, 2011 Share Posted January 26, 2011 $str = "Animal Kids Basic Hoody Blue"; if ( (strstr($str, "Kids") != false) && (strstr($str, "Hoody") != false) ) { //do something } Link to comment https://forums.phpfreaks.com/topic/225734-match-2-words-in-string/#findComment-1165460 Share on other sites More sharing options...
Cagecrawler Posted January 26, 2011 Share Posted January 26, 2011 <?php function containsAll($keywords, $haystack) { foreach($keywords as $key) { if(!in_array($key, $haystack)) { return false; } } return true; } $keywordInput = "Kids,Hoody"; $keywords = explode(',', $keywordInput); $haystackInput = "Animal Kids Basic Hoody Blue"; $haystack = explode(' ', $haystackInput); if(containsAll($keywords, $haystack)) { echo "All terms included"; } else { echo "Not all terms included"; } Link to comment https://forums.phpfreaks.com/topic/225734-match-2-words-in-string/#findComment-1165462 Share on other sites More sharing options...
PaulRyan Posted January 26, 2011 Share Posted January 26, 2011 <?PHP $str = "Animal Kids Basic Hoody Blue"; $doesExist = strstr(strtolower($str),'kids') && strstr(strtolower($str),'hoody') ? 'Existant' : 'None Exisant'; echo $doesExist; ?> Try the above to, works for me Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/225734-match-2-words-in-string/#findComment-1165466 Share on other sites More sharing options...
gvp16 Posted January 26, 2011 Author Share Posted January 26, 2011 Thanks very much guys, done what I needed. Link to comment https://forums.phpfreaks.com/topic/225734-match-2-words-in-string/#findComment-1165476 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.