'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 1:[1]
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;
}
}
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;
}
Solution 2:[2]
The issue is that get_meta_tags is returning an array and you can't have duplicate array keys.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Guido Faecke |
