Reputation: 3771
I'm using the following preg_match to get the [title] tag of a webpage.
// get <title>
$search = "/\<title\>(.*)\<\/title>/i";
preg_match($search, $url_contents, $result);
$title = $result[1];
Now I want to search inside that [title] tag, so I wrote this:
// search for $keyword
$keyword_slash = "/". $keyword ."/";
preg_match_all($keyword_slash, $title, $result);
print_r($result); // just for testing
I've added $keyword_slash because else it gave me an error. Yet, this doesn't work. It always returns an empty array even though I know that $keyword is inside the [title].
Upvotes: 1
Views: 734
Reputation: 785156
Your regex first preg_match doesn't seem to be right. It should be:
$search = "~<title>([^<]*)</title>~i";
However I must remind you that extracting title using regex like this is extremely error prone and you should consider using DOM parser to get that.
Update: Here is the suggested DOM parsing for title of a page:
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$title = $xpath->query("//head/title")->item(0)->nodeValue;
printf("title=[%s]\n", $title);
Upvotes: 3