PHP get_meta_tags() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
URL

Definition

In PHP, get_meta_tags() reads the <meta> tags in the head of an HTML file or web page and returns them as an array.

The meta name becomes the array key, in lower case, and the content becomes the value.

Syntax

Call get_meta_tags() with the values you need. Start with this line.

PHP
$tags = get_meta_tags("page.html");
CallWhat it does
get_meta_tags("page.html")Returns the meta tags of a local file.
get_meta_tags("https://example.com")Returns the meta tags of a web page.

Only tags with a name and content are read. Parsing stops at the closing </head> tag.

Library declarations
PHP
get_meta_tags(string $filename, bool $use_include_path = false): array|false

Parameters

ParameterDescription
filenameRequired. A file path or a URL of the HTML to read.
use_include_pathOptional. Set to true to search the include_path too. Default is false.

Return Value

Return ValueDescription
array|falseAn associative array of meta tags, or false on failure.

PHP Examples

Example 1 — Read Meta Tags from a File

This demo works offline. It writes a small HTML file and then reads its meta tags.

PHP
<?php
$html = '<html><head>
<meta name="author" content="Mari">
<meta name="keywords" content="php, tutorial">
<meta name="description" content="Learn PHP">
</head><body></body></html>';

$file = sys_get_temp_dir() . "/demo-meta.html";
file_put_contents($file, $html);

print_r(get_meta_tags($file));

How It Works

  1. Create a small HTML page. It has three meta tags.
  2. Save it to a temporary file. file_put_contents() writes the text.
  3. Call get_meta_tags() with the path. It reads only the head section.
  4. Read the result. Each meta name is a key and its content is the value.

Example 2 — See How Names Are Changed

Names are lower-cased and special characters become underscores.

PHP
<?php
$html = '<head>
<meta name="Robots" content="index, follow">
<meta name="geo.position" content="10.5;76.2">
</head>';

$file = sys_get_temp_dir() . "/demo-meta2.html";
file_put_contents($file, $html);

print_r(get_meta_tags($file));

How It Works

  1. Use a name with capital letters. Robots becomes robots.
  2. Use a name with a dot. geo.position becomes geo_position.
  3. Use the new keys. Look up $tags["geo_position"] in your code.
  4. Keep the content as is. Only the names are changed.

Example 3 — Read the Meta Tags of a Web Page

Pass a URL to read a live page. The output below is a typical sample, because real values depend on the site.

PHP
<?php
$tags = get_meta_tags("https://example.com");

if ($tags === false) {
    exit("Could not read the page");
}

echo $tags["description"] ?? "No description", PHP_EOL;

How It Works

  1. Pass a URL. This needs allow_url_fopen to be on.
  2. Check for false. The page may be offline or blocked.
  3. Read a tag by name. The null coalescing ?? handles a missing tag.
  4. Expect different text. The output shown is a typical sample.

Frequently Asked Questions

get_meta_tags() reads the meta tags from the head of an HTML page or file and returns them as an associative array.
get_meta_tags(string $filename, bool $use_include_path = false): array|false. Pass a file path or a URL.
PHP lower-cases the name and replaces non-alphanumeric characters with underscores, so geo.position is read as geo_position.
Only tags that use a name attribute are read. Open Graph tags use property instead, so they are not returned.
Yes, if allow_url_fopen is enabled and PHP has OpenSSL support. It returns false if the page cannot be opened.

Next: http_build_query()

http_build_query() builds a URL query string from an array.

http_build_query() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful