
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.
$tags = get_meta_tags("page.html");
| Call | What 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
get_meta_tags(string $filename, bool $use_include_path = false): array|false
Parameters
| Parameter | Description |
|---|
| filename | Required. A file path or a URL of the HTML to read. |
| use_include_path | Optional. Set to true to search the include_path too. Default is false. |
Return Value
| Return Value | Description |
|---|
| array|false | An 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
$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));
Array
(
[author] => Mari
[keywords] => php, tutorial
[description] => Learn PHP
)How It Works
- Create a small HTML page. It has three meta tags.
- Save it to a temporary file.
file_put_contents() writes the text. - Call get_meta_tags() with the path. It reads only the head section.
- 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
$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));
Array
(
[robots] => index, follow
[geo_position] => 10.5;76.2
)How It Works
- Use a name with capital letters.
Robots becomes robots. - Use a name with a dot.
geo.position becomes geo_position. - Use the new keys. Look up
$tags["geo_position"] in your code. - Keep the content as is. Only the names are changed.
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.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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