get meta description tag with xpath

后端 未结 5 1964
生来不讨喜
生来不讨喜 2021-02-07 06:39

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

$str = <<< EOD



        
5条回答
  •  别那么骄傲
    2021-02-07 07:11

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

提交回复
热议问题