Home > database >  Retrieve a meta with the same name with get_meta_tags() function
Retrieve a meta with the same name with get_meta_tags() function

Time:04-30

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 ?

CodePudding user response:

The issue is that get_meta_tags is returning an array and you can't have duplicate array keys.

CodePudding user response:

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

  • Related