get_meta_tags

Extracts the name and content of every meta tag in an HTML document's head.

Signature
get_meta_tags(string $filename, bool $use_include_path = false): array|false
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$filenamestringPath to the HTML file, or a URL when remote wrappers are enabled.
$use_include_pathoptionalbool= falseWhen true, PHP also searches the include_path for the file. Applies to local files only, not URLs.

Return value

array|falseAn associative array mapping each meta tag's lowercased name to its content, or false on failure.

Errors

Does not throw. It emits an E_WARNING and returns false if the file cannot be opened.

get_meta_tags() reads an HTML document line by line, collects every <meta> tag it finds, and stops at </head>. Each tag's name attribute becomes the array key and its content attribute the value.

Key mangling

Keys are lowercased, and any character that is not valid in a PHP variable name is replaced with an underscore. So <meta name="geo.position"> arrives as geo_position, and og:title would become og_title — except that it never arrives at all, because of the next point.

Only name, never property

The parser looks exclusively at the name attribute. Open Graph and Twitter Card tags use property and are silently skipped, which is the number one reason people conclude the function is broken. Nor does it read <title>, <link rel="canonical">, or http-equiv tags.

When to use something else

For Open Graph, canonical links, or anything past the head, load the page with file_get_contents() and parse it with DOMDocument plus DOMXPath. Reach for get_meta_tags() when you want a quick description and keywords from a local file and nothing more. Duplicate names collapse: only the last occurrence survives.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Pull the description and keywords out of a saved HTML file for an internal index
  • Check that generated pages actually contain the meta description you expect
  • Read author and robots directives from a batch of static templates
  • Audit a directory of exported pages for missing descriptions

Frequently asked questions

Why are my Open Graph tags missing from get_meta_tags?

It only parses meta tags that use a name attribute, and Open Graph uses property. Parse the document with DOMDocument and DOMXPath instead.

Can get_meta_tags read a remote URL?

Yes, if allow_url_fopen is enabled, but it does a plain unconfigurable fetch. Use file_get_contents with a stream context, or cURL, when you need timeouts or headers.

Why is a key named geo_position instead of geo.position?

Characters that are invalid in a PHP variable name are replaced with underscores, and the whole key is lowercased.

More examples

Duplicates and missing tags

Only index,follow survives — the second robots tag overwrites the first. The tag placed after </head> is not collected at all, and the ?? guard shows the safe way to read a key that may simply not exist.

Example · php
<?php
file_put_contents('dup.html', '<head>'
    . '<meta name="robots" content="noindex">'
    . '<meta name="robots" content="index,follow">'
    . '</head><meta name="late" content="after head">');

$tags = get_meta_tags('dup.html');
print_r($tags);
echo 'description: ' . ($tags['description'] ?? 'MISSING') . "\n";

unlink('dup.html');

Auditing a batch of files for a description

b.html has a title but no meta description, so it is flagged. The ?: [] guards against get_meta_tags returning false on an unreadable file, which would otherwise make the array access fail.

Example · php
<?php
file_put_contents('a.html', '<head><meta name="description" content="Page A"></head>');
file_put_contents('b.html', '<head><title>Page B</title></head>');

foreach (['a.html', 'b.html'] as $file) {
    $tags = get_meta_tags($file) ?: [];
    printf("%-8s %s\n", $file, $tags['description'] ?? '*** no description ***');
}

unlink('a.html');
unlink('b.html');

Discussion

  • Be the first to comment on this lesson.