PHP get_meta_tags() Function
The PHP get_meta_tags() function opens filename and parses it line by line for <meta> tags in the file and returns an array with all the parsed meta tags. The parsing stops at </head>.
Syntax
get_meta_tags(filename, use_include_path)
Parameters
filename |
Required. Specify the path to the HTML file, as a string. This can be a local file or an URL. |
use_include_path |
Optional. Setting this parameter to true, will result into PHP trying to open the file along the standard include path as per the include_path directive. This is used for local files, not URLs. include_path can be set in php.ini. |
Return Value
Returns an array with all the parsed meta tags. The value of the name property will become the key, the value of the content property will become the value of the returned array. Special characters in the value of the name property are substituted with '_', the rest is converted to lower case. If two meta tags have the same name, only the last one is returned. Returns false on failure.
Example: get_meta_tags() example
Lets assume that we have a file called https://www.alphacodingskills.com/index.php. This file contains following <meta> tags.
<meta name="author" content="AlphaCodingSkills"> <meta name="keywords" content="Tutorials on various programming languages"> <meta name="DESCRIPTION" content="An online learning portal"> <meta name="geo.position" content="47.4925; 19.0513"> </head> <!-- parsing stops here -->
The example below describes how the get_meta_tags() function is used to parses this file line by line for <meta> tags and returns an array with all the parsed meta tags.
<?php //assuming the above tags at https://www.alphacodingskills.com/index.php $tags = get_meta_tags('https://www.alphacodingskills.com/index.php'); //please note that the keys are all lowercase //and . is replaced by _ in the key echo $tags['author']."\n"; echo $tags['keywords']."\n"; echo $tags['description']."\n"; echo $tags['geo_position']."\n"; ?>
The output of the above code will be:
AlphaCodingSkills Tutorials on various programming languages An online learning portal 47.4925; 19.0513
❮ PHP URLs Reference