帮助算法确定PHP中字符串中的短语出现

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?

Read the docs about substr_count. Use it in a loop over $words.

 $res = array();
 foreach($words as $word){
    $res[$word] = substr_count($sentence,$word);
 }

something like this would do it.

$res = array();
foreach($words as $word){
  $res[$word] = preg_match_all("/{$word}/i", $sentence);
}

note: since it's using regular expression you have to make sure your word don't have regular expression symbols and escape them, also a solution based on str_pos might perform better so it depends the number of sentence you have to analyze and number of words involved.

using @Ofri solution

$res = array();
foreach($words as $word){
  $res[$word] = substr_count($sentence,$word);
}

This is known as entity extraction in Natural Language Processing. It may look simple in your example but it can grow quite complex. If you are going to be using it seriously you should consider looking at toolkits which do this such as NLTK, OpenNLP and Lucene.

Here's another regex implementation:

$words = array('barack obama', 'chicago', 'united states');
$sentence = "Barack Obama is from Chicago. Barack Obama's favorite food it pizza. He is president of the United States";
$re= sprintf('/(%s)/i', implode('|',  $words));
if (preg_match_all($re, $sentence, $m))
 print_r(array_count_values($m[0]));

Easy to extend - just update $words and $sentence with whatever you want.