To retrieve the title, I use :
$titles = $xpath->query('/html/head/title');
foreach ($titles as $title) {
$return['title'][] = $title->nodeValue;
}
Because it's possible that the DOM has multiple <title />
tags, though it is not recommended.
I then parse all the <meta />
tags, as such :
$metas = $xpath->query('/html/head/meta');
foreach ($metas as $meta) {
$name = strtolower($meta->getAttribute('name'));
if ($name == 'title' && (!isset($return['title']) || $return['title'] == '')) // There is such a thing as <meta name="title" \>
$return['title'][] = $meta->getAttribute('content');
if ($name == 'keywords')
$return['metakw'][] = $meta->getAttribute('content');
if ($name == 'description')
$return['metadesc'][] = $meta->getAttribute('content');
if ($name == 'robots')
$return['robots'][] = $meta->getAttribute('content');
}
But I guess it would be possible to use :
$metas_title = $xpath->query('/html/head/meta[@name="title"]');
$metas_desc = $xpath->query('/html/head/meta[@name="description"]');
$metas_kw = $xpath->query('/html/head/meta[@name="keywords"]');
$metas_robots = $xpath->query('/html/head/meta[@name="robots"]');