Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get meta description tag with xpath

Tags:

php

xpath

I need the content the description and the keywords tag content. I have this code, but dont write anything. Idea?

$str = <<< EOD

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta name="description" content="text in the description tag" />

<meta name="keywords" content="text, in, the, keywords, tag" />

</head>

EOD;
$dom = new DOMDocument();

$dom->loadHTML($str);

$xpath = new DOMXPath($dom);
$nodes = $xpath->query('/html/head/meta[name="description"]');

foreach($nodes as $node){
  print $node->nodeValue;
}
like image 674
turbod Avatar asked May 25 '10 18:05

turbod


People also ask

What is metatag description?

A meta description tag generally informs and interests users with a short, relevant summary of what a particular page is about. They are like a pitch that convince the user that the page is exactly what they're looking for.

What is content metatag?

Definition and Usage. The <meta> tag defines metadata about an HTML document. Metadata is data (information) about data. <meta> tags always go inside the <head> element, and are typically used to specify character set, page description, keywords, author of the document, and viewport settings.


4 Answers

You can reference the attributes using @ followed by the attribute name (see below), and you can query directly for the attributes; your XPath query was almost there.

// Look for the content attribute of description meta tags 
$contents = $xpath->query('/html/head/meta[@name="description"]/@content');

// If nothing matches the query
if ($contents->length == 0) {
    echo "No description meta tag :(";
// Found one or more descriptions, loop over them
} else {
    foreach ($contents as $content) {
        echo $content->value . PHP_EOL;
    }
}
like image 63
salathe Avatar answered Oct 29 '22 01:10

salathe


You have two problems. First, name is an attribute so you need to prepend @,

$nodes = $xpath->query('/html/head/meta[@name="description"]');

Second, the nodes are all empty so there is nothing to print.

To print the attribute value, do this,

foreach($nodes as $node){
  $attr = $node->getAttribute('content');
  print $attr;
}
like image 45
ZZ Coder Avatar answered Oct 29 '22 01:10

ZZ Coder


In stead of including the /html/head part you could also use double slash which means that the following node can be anywhere in the code:

//meta[@name='description']

Will give the same result as:

/html/head/meta[@name='description']

Doesn't really matter much but it's less typing...

like image 40
user3733663 Avatar answered Oct 29 '22 00:10

user3733663


Last but not least, and sorry for reviving this thread, the queries are case sensitive.

In other words, if you look for meta name="description"... or "meta name="keywords", it will not find "meta name="Description"... or "meta name="Keywords"... respectively. So careful with that!

And I can tell you, after working a while with xdom and metatags, eventually I believe that the best approach for that is to use this function: http://php.net/manual/es/function.get-meta-tags.php

like image 29
Chris Russo Avatar answered Oct 29 '22 00:10

Chris Russo