I have a piece of PHP code as follows:
$words = array(
'Artist' => '1',
'Tony' => '2',
'Scarface' => '3',
'Omar' => '4',
'Frank' => '5',
'Torrentino' => '6',
'Mel Gibson' => '7',
'Frank Sinatra' => '8',
'Shakes' => '9',
'William Shakespeare' => '10'
);
$text = "William Shakespeare is very famous in the world. An artist is a person engaged in one or more of any of a broad spectrum of activities related to creating art, practicing the arts, and/or demonstrating an art. Artist is a descriptive term applied to a person who engages in an activity deemed to be an art. Frank Sinatra was an American singer, actor, director, film producer, and conductor. Frank Sinatra was born on December 12, 1915, in Hoboken, New Jersey, the only child of Italian immigrants Natalina Garaventa and Antonino Martino Sinatra, and was raised Roman Catholic.";
$re = '/\b(?:' . join('|', array_map(function($keyword) {
return preg_quote($keyword, '/');
}, array_keys($words))) . ')\b/i';
preg_match_all($re, $text, $matches);
foreach ($matches[0] as $keyword) {
echo $keyword, " ", $words[$keyword], "\n";
}
The code returns the below:
William Shakespeare 10 artist Artist 1 Frank 5 Frank 5
The code works well to not echo the partial keywords such as 'Shakes' => '9' in Shakespeare. However, as you can see the code can not detect 'Frank Sinatra' => '8' as a keyword as it is in Frank Sinatra was an American singer, also artist does not have any value (i.e. 1). Could you please help me to change the code somehow to echo William Shakespeare 10 artist 1 Artist 1 Frank 5 Frank 5 Frank Sinatra 8 Frank Sinatra 8 instead of William Shakespeare 10 artist Artist 1 Frank 5 Frank 5 in the current version. thanks for your help.
William 9 World Cup 4 William Shakespeare 8and notWorld Cup 4 William Shakespeare 8so it seems it works as you want