I am trying to detect if any keywords assigned to an article also exist in the title, with a goal of then removing that keyword from $keywords. Here is what I’ve come up with but it doesn’t work. Any thoughts of what I’m overlooking?
Thanks!
$title = "The Rash is Gone";
$keywords = "lavender, skin, rash, rashes";
// Remove any commas
$keywords = str_replace(',','',$keywords);
// Convert to lowercase
$title = strtolower($title);
$keywords = strtolower($keywords);
// Create arrays
$titleArray = explode(" ", $title);
$keywordsArray = explode(" ", $keywords);
$matches = array();
$noMatches = true;
print ("Title: $title <br />");
print ("Keywords: $keywords <br />");
foreach ($keywordsArray as $individualKeyword) {
if (preg_match("/\b$individualKeyword\b/i", $titleArray)) {
array_push($matches,$individualKeyword);
$noMatches = false;
$wordCount++;
}
}
if (!$noMatches) {
$wordList = implode(', ', $matches);
$wordList = rtrim($wordList, ', ');
if ($wordCount > 1) {
print ("Matched words: $wordList");
} else {
print ("Matched word: $wordList");
}
} else {
print ("<p>No keywords were found in the title.</p>");
}