php 使用 xpath 获取元描述标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2907478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:05:10  来源:igfitidea点击:

get meta description tag with xpath

phpxpath

提问by turbod

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;
}

回答by salathe

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.

您可以使用@后跟属性名称(见下文)来引用属性,并且您可以直接查询属性;您的 XPath 查询几乎就在那里。

// 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;
    }
}

回答by ZZ Coder

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;
}

回答by user3733663

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:

除了包含 /html/head 部分,您还可以使用双斜杠,这意味着以下节点可以位于代码中的任何位置:

//meta[@name='description']

Will give the same result as:

将给出与以下相同的结果:

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

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

没什么大不了的,但打字少了...

回答by Chris Russo

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!

换句话说,如果您查找 meta name="description"... 或 "meta name="keywords",它不会找到 "meta name="Description"... 或 "meta name="Keywords".. . 分别. 小心点!

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

我可以告诉你,在使用 xdom 和 metatags 一段时间后,最终我相信最好的方法是使用这个函数:http: //php.net/manual/es/function.get-meta-tags。 php

回答by Chris Russo

Make sure that you put EOD;at a line without any spaces and indentation like:

确保EOD;在一行中没有任何空格和缩进,例如:

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

EOD;