php

Retrieve a meta with the same name with get_meta_tags() function


I'm trying to retrieve the <meta name="generator"> of a web page with the php get_meta_tags() function.

Worry is that my web page contains TWO same meta :

<meta name="generator" content="WordPress 5.9.3">
<meta name="generator" content="Site Kit by Google 1.73.0">

And get_meta_tags() seems to only want to retrieve the last when I want the first.

Here is my code :

$fullURL = 'https://thibautchourre.com/';
$metas = get_meta_tags($fullURL);
$versionwp = $metas['generator']
echo $versionwp

An idea ?


Solution

  • The get_meta_tags function only returns the last element when the name is duplicated.

    If two meta tags have the same name, only the last one is returned

    You could use a parser to get all meta elements.

    Sample:

    <?php
    $html = '<meta name="generator" content="WordPress 5.9.3">
    <meta name="generator" content="Site Kit by Google 1.73.0">
    ';
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $metas = $doc->getElementsByTagName('meta');
    foreach($metas as $meta){
        if($meta->getAttribute('name') == 'generator'){
            echo $meta->getAttribute('content') . PHP_EOL;
        }
    }
    

    https://3v4l.org/jDeNo

    Alternative using xpath, (adapt from get meta description tag with xpath)

    $html = '<meta name="generator" content="WordPress 5.9.3">
    <meta name="generator" content="Site Kit by Google 1.73.0">
    ';
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xpath = new DOMXPath($doc);
    $nodes = $xpath->query('//meta[@name="generator"]/@content');
    foreach($nodes as $node){
        echo $node->textContent . PHP_EOL;
    }