smerny Posted May 30, 2010 Share Posted May 30, 2010 Hey, if I have an array of words and I want to check a string to see if any of the words in the array are also in the string... what is the best way to do that? I don't need to know exactly which word is in it, and I do want the words to be found even if they are just a part of a word (super would be found in superman). Link to comment https://forums.phpfreaks.com/topic/203316-does-string-have-any-words-from-this-array/ Share on other sites More sharing options...
smerny Posted May 30, 2010 Author Share Posted May 30, 2010 i know that i could do a "foreach" and check if (strpos(strtolower($message), $word)... but i wonder if there is a function made for doing something like this? Link to comment https://forums.phpfreaks.com/topic/203316-does-string-have-any-words-from-this-array/#findComment-1065205 Share on other sites More sharing options...
ZachMEdwards Posted May 30, 2010 Share Posted May 30, 2010 function FindWords($needle, $haystack) { $out = ''; foreach($item as $haystack) if(strpos($item, $needle) !== FALSE) $out .= $item.', '; return @substr($out, 0, strlen($out) - 2); } I don't know of anyother ways. Link to comment https://forums.phpfreaks.com/topic/203316-does-string-have-any-words-from-this-array/#findComment-1065211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.