etrader Posted April 16, 2011 Share Posted April 16, 2011 I want to check if a string has any of words listed in array; $array=array('word1', 'word2', 'word3'); $string="This is a sentence containing word2"; Now I want to make an 'if else' to check if the string contains any of the words listed in the array. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/233875-how-to-check-a-string-for-words-in-an-array/ Share on other sites More sharing options...
AtomicRax Posted April 16, 2011 Share Posted April 16, 2011 foreach ($array as $var) { if(strstr($string, $var)) { // the $string contains $var from the $array } else { // the $string does NOT contain $var from the array } } Quote Link to comment https://forums.phpfreaks.com/topic/233875-how-to-check-a-string-for-words-in-an-array/#findComment-1202243 Share on other sites More sharing options...
QuickOldCar Posted April 16, 2011 Share Posted April 16, 2011 Might be better off using the case insensitive version if looking for words. stristr() http://www.php.net/manual/en/function.stristr.php Quote Link to comment https://forums.phpfreaks.com/topic/233875-how-to-check-a-string-for-words-in-an-array/#findComment-1202268 Share on other sites More sharing options...
AtomicRax Posted April 16, 2011 Share Posted April 16, 2011 QuickOldCar has a point .. I just had this snippet readily available because I'm currently using it myself.. but I have to match the case for mine program.. you can easily modify my post with the new function Quote Link to comment https://forums.phpfreaks.com/topic/233875-how-to-check-a-string-for-words-in-an-array/#findComment-1202271 Share on other sites More sharing options...
etrader Posted April 16, 2011 Author Share Posted April 16, 2011 Thank fella, How to avoid foreach by defining a string ($var) containing the words as $var='word1|word2|word3' to check if any value matches. Quote Link to comment https://forums.phpfreaks.com/topic/233875-how-to-check-a-string-for-words-in-an-array/#findComment-1202273 Share on other sites More sharing options...
sasa Posted April 16, 2011 Share Posted April 16, 2011 <?php $array=array('word1', 'word2', 'word3'); $string="This is a sentence containing word2"; if(preg_match('/\b'. implode('\b|\b', $array).'\b/i', $string)) echo 'yes'; else echo 'no'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/233875-how-to-check-a-string-for-words-in-an-array/#findComment-1202274 Share on other sites More sharing options...
etrader Posted April 16, 2011 Author Share Posted April 16, 2011 Seems to be a subtle solution, but the output in all cases is "no" Quote Link to comment https://forums.phpfreaks.com/topic/233875-how-to-check-a-string-for-words-in-an-array/#findComment-1202275 Share on other sites More sharing options...
QuickOldCar Posted April 16, 2011 Share Posted April 16, 2011 There's a few ways to check for items, it's all in how you set it up..what you are looking for..and what kind or results you expect. These are things you have to do unique to your project with making some checks and balances somehow. Sometimes you need to use a mixture to get the exact desired results. You may even have to explode the array and a foreach just to clean it up a bit before checking it. some useful stuff I usually stick to preg_match,preg_match_all,preg_quote,in_array for the most part. http://php.net/manual/en/function.preg-match.php http://www.php.net/manual/en/function.preg-match-all.php http://www.php.net/manual/en/function.preg-quote.php http://php.net/manual/en/function.array-search.php The code that sasa gave is most likely what you need, you need to adjust the pattern for how you exactly want to look for items. Quote Link to comment https://forums.phpfreaks.com/topic/233875-how-to-check-a-string-for-words-in-an-array/#findComment-1202285 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.