Help with algorithm to determine phrase occurrence in string in PHP
- by Will M.
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?