get meta description tag with xpath

后端 未结 5 1960
生来不讨喜
生来不讨喜 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:08

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

提交回复
热议问题