Help with algorithm to determine phrase occurrence in string in PHP

Posted by Will M. on Stack Overflow See other posts from Stack Overflow or by Will M.
Published on 2009-11-25T07:27:05Z Indexed on 2010/04/06 18:53 UTC
Read the original article Hit count: 175

Filed under:

I have an array of phrases (max 2 words) like

$words = array('barack obama', 'chicago', 'united states');

and then I have a string like:

$sentence = "Barack Obama is from Chicago. Barack Obama's favorite food it pizza.";

I want to find/create an efficient algorithm that would return the number of occurrences of the words in the array $words in the string $sentence. In this case it would be:

'barack obama' => 2
'chicago' => 0

How can I built this?

© Stack Overflow or respective owner

Related posts about php