Reputation: 1
Can anyone help me here? I’m trying to make a program that checks if a keyword is found on the title of a website. Like the keywords are stored in a variable $keywords and I'd like to check if they exist in $title. The keywords can vary in numbers and they are usually separated by commas. How do I do that? I'm using php by the way. Thanks in advance!
Upvotes: 0
Views: 1050
Reputation: 3870
To check if a word is found in a string:
function found_keyword($keyword, $title) {
return preg_match('/' . $keyword . '/', $title);
}
(I used preg_match because you had that tag in your question, but I guess there are simpler functions to just check the occurence of a substring in a string)
To check if at least one word in an array of words is found in a string:
function found_keywords($keywords, $title) {
return array_reduce($keywords, function ($match_found, $keyword) use ($title) {
return $match_found || found_keyword($keyword, $title);
}, FALSE);
}
To create an array of keywords from a string of comma-separated words:
$keywords = split(',', $comma_separated_string);
Upvotes: 1
Reputation: 342
This link might be usefull for you Grabbing title of a website using DOM
After you get title as string you can simply use explode function
Upvotes: 0