PHP - URL get_meta_tags() Function



The PHP URL get_meta_tags() function is used to extract all meta tag content attributes from the given URL and can return an array. This function can open filename and parse it line by line for <meta> tags in a file. The parsing stops at </head>.

The value of the name property becomes key, the value of the content property becomes the value of the returned array, so we can easily use standard array functions to traverse it or access single values. The special characters in the value of name property are substituted with "_", the rest is converted to lower case. If two meta tags with the same name, only the last one is returned.

Syntax

Below is the syntax of the PHP URL get_meta_tags() function −

array get_meta_tags ( string $filename [, bool $use_include_path = false ] )

Parameters

Below are the parameters of the get_meta_tags() function −

  • $filename − It is the URL or file path from which you need to extract the meta tags.

  • $use_include_path − It is a boolean value. If it is set to true, the function will also search for the file in the include path.

Return Value

This function returns an array with all the parsed meta tags. If there are no meta tags or an error occurs, it will return an empty array.

PHP Version

The get_meta_tags() function introduced in core PHP 4 and continues to function in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP URL get_meta_tags() function to get the information about meta tag content from the given URL.

<?php
   // Define url here
   $url = 'http://www.tutorix.com';
   $meta_tags = get_meta_tags($url);
   
   print_r($meta_tags);
?>

Output

The above code will result something like this −

Array
(
    [viewport] => initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width
    [description] => Affordable Premium Learning - CBSE and State Boards, the amazing learning app which brings you simply easy learning at your mobile and tabs. The best online learning app for 6th - 12th Classes, NEET and IIT/JEE Exams
)

Example 2

This PHP example will show you how you can get the meta tag content from the local HTML file with the help of get_meta_tags() function.

<?php
   // Define file path here
   $filename = '/PHP/PhpProjects/myhtml.htm';
   $meta_tags = get_meta_tags($filename);
   
   print_r($meta_tags);
?> 

Output

This will generate the below output −

Array
(
    [viewport] => width=device-width, initial-scale=1.0
)

Example 3

Now the below code try to handle no meta tags in the case when no meta tags are found using get_meta_tags() function.

<?php
   /$url = 'http://www.example.com';
   $meta_tags = get_meta_tags($url);
   
   if (empty($meta_tags)) {
       echo 'No meta tags found.';
   } else {
       print_r($meta_tags);
   }
?> 

Output

This will create the below output −

Array
(
    [viewport] => width=device-width, initial-scale=1.0
)

Summary

The get_meta_tags() function is a built-in method in PHP used to get the content present in the meta tags. So we have seen different examples to see how to use this function to get meta information from different sources.

php_function_reference.htm
Advertisements