0

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!

Wyan
  • 1
  • 1

2 Answers2

1

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);
Ygg
  • 3,798
  • 1
  • 17
  • 23
  • thanks! i tried these but i get errors. Parse error: syntax error, unexpected T_FUNCTION – Wyan Jun 19 '13 at 05:44
  • It depends on where you declare those functions; I pasted them into an empty PHP-documents and they worked for me. Maybe if you have a PHP version that doesn't support anonymous functions it can protest too, since there is one such in the found_keyword's array_reduce. – Ygg Jun 19 '13 at 05:55
0

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

Community
  • 1
  • 1
ujjwalwahi
  • 342
  • 1
  • 4
  • 13