Basically, I have an array of keywords, and a piece of text. I am wondering what would be the best way to find out if any of those keywords are present in the text, bearing in m
Depending on the size of the string You could use a hash to make it faster.
First iterate the text. For each word, assign it to an array:
foreach (preg_split("/\s/", $text) as $word)
{
$string[$word] = 1;
}
Then iterate the keywords checking the $string:
foreach ($keywords as $keyword)
{
if (isset($string[$keyword]))
{
// $keyword exists in string
}
}
EDIT If your text is much smaller than your keywords, do it backwards, check the keywords for each word in the text. This would likley be faster than the above if the text is pretty short.
foreach (preg_split("/\s/", $text) as $word)
{
if (isset($keywords[$word]))
{
//might be faster if sizeof($text) < sizeof($keywords)
}
}