Checking if any assigned keywords exist in document title

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?


$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)) {
	    $noMatches = false;

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>");

why not using array_intersect() (matches) or array_diff() (non-matches) when you’ve already broken it down into arrays?

ah, yes, and preg_match() works on strings, not arrays.

array_intersect is just what I needed. Thanks Dormilich!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.