Content · Programming · PHP

PHP get_meta_tags() Function: Usage and Examples

Understand PHP get_meta_tags() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use get_meta_tags() when you need to extracts named meta elements from an HTML document read from a path or URL in a PHP application. The examples below show the actual operation and its limitations.

What Is get_meta_tags() in PHP?

Extracts named meta elements from an HTML document read from a path or URL. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

get_meta_tags(string $filename, bool $use_include_path = ...): array|false

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

$file = tempnam(sys_get_temp_dir(),"meta_"); file_put_contents($file,"<meta name=\"description\" content=\"Sample\">"); print_r(get_meta_tags($file)); unlink($file);

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Example 2: Different Input or Behavior

<?php

$file = tempnam(sys_get_temp_dir(),"meta_"); file_put_contents($file,"<meta name=\"author\" content=\"Taylor\">"); echo get_meta_tags($file)["author"]; unlink($file);

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Practical Example: Applying the Function

<?php

$file = tempnam(sys_get_temp_dir(),"meta_"); file_put_contents($file,"<html><head><meta name=\"description\" content=\"Catalog\"></head></html>"); $tags = get_meta_tags($file); echo $tags["description"] ?? "Missing"; unlink($file);

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Important Behavior and Edge Cases

This is not a full HTML parser, and untrusted URLs can introduce SSRF risk.

Comparison With Related Functions

ApproachDifference
get_meta_tags()Extracts named meta elements from an HTML document read from a path or URL.
Related operationDOMDocument can parse HTML structures more broadly; get_meta_tags() extracts named meta elements.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationThis is not a full HTML parser, and untrusted URLs can introduce SSRF risk.
Confusing this function with a related operationDOMDocument can parse HTML structures more broadly; get_meta_tags() extracts named meta elements.

Key Takeaways

  • Extracts named meta elements from an HTML document read from a path or URL.
  • This is not a full HTML parser, and untrusted URLs can introduce SSRF risk.
  • DOMDocument can parse HTML structures more broadly; get_meta_tags() extracts named meta elements.

Use get_meta_tags() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

Share this resourceSend this page to someone who may find it useful.