I have a PHP code snippet as follows:
$words = array(
'Art' => '1',
'Sport' => '2',
'Big Animals' => '3',
'World Cup' => '4',
'David Fincher' => '5',
'Torrentino' => '6',
'Shakes' => '7',
'William Shakespeare' => '8'
);
$text = "I like artists, and I like sports. Can you call the name of a big animal? Brazil World Cup matchers are very good. William Shakespeare is very famous in the world.";
$all_keywords = $all_keys = array();
foreach ($words as $word => $key) {
if (strpos(strtolower($text), strtolower($word)) !== false) {
$all_keywords[] = $word;
$all_keys[] = $key;
}
}
echo $keywords_list = implode(',', $all_keywords) ."<br>";
echo $keys_list = implode(',', $all_keys) . "<br>";
Echos code Art,Sport,World Cup,Shakes,William Shakespeareand 1,2,4,7,8; however, the code is very simple and not accurate enough to repeat the correct keywords. For example, the code returns 'Shakes' => '7'because of the word Shakespearein $text, but, as you can see, Shakes cannot represent Shakespeare as the correct keyword. Mostly I want to return Art,Sport,World Cup,William Shakespeareand 1,2,4,8instead of Art,Sport,World Cup,Shakes,William Shakespeareand 1,2,4,7,8. So, could you help me develop the best code to extract keywords without problems with similar problems? thank you for your help.