(pay attention to line endings - PHP uses a native function to
parse the input, so a Mac file won't work on Unix).

use_include_path

Setting use_include_path to TRUE will result
in 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.

Return Values

Returns an array with all the parsed meta tags.

The value of the name property becomes the key, the value of the content
property becomes the value of the returned array, so you can easily use
standard array functions to traverse it or access single values.
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.

Examples

Example #2 What get_meta_tags() returns

<?php// Assuming the above tags are at www.example.com$tags = get_meta_tags('http://www.example.com/');

See Also

User Contributed Notes 19 notes

This regex gets meta tags independent of sequence by capturing inside a lookahead.Further uses the branch reset feature for different quote styles of values.The pattern can be tested here: https://regex101.com/r/oE4oU9/1

I tried using the regex displayed in his code, and php threw up a couple of errors

Below is the correct regular expression that works(Please note that I had to split the regex into strings because php.net was complaining about the line being to long)<?phppreg_match_all("|<meta[^>]+name=\"([^\"]*)\"[^>]" . "+content=\"([^\"]*)\"[^>]+>|i",$html, $out,PREG_PATTERN_ORDER);?>

The problem was due to the quotes being incorrectly escaped.I hope this helps anyone who has been having problems with his code

New version based on mariano at cricava dot com's work with:1) Support for Meta properties (like Facebook's og tags).2) Support for Unicode (UTF-8) encoded Meta lines.3) An option not to convert htmlentities - if you plan to actually use the results and not just display them.

Based on Michael Knapp's code, and adding some regex, here's a function that will get all meta tags and the title based on a URL. If there's an error, it will return false. Using the function getUrlContents(), also included, it takes care of META REFRESH re-directions, following up to the specified number of redirections. Please note that the regular expressions included were split into strings because php.net was complaining about the line being to long ;)

get_meta_tags() seems to look only in the beginning of a file, meaning that e.g. if there is a lot of PHP code before the HTML header it will return nothing ...
Tested using get_meta_tags() on local files with about 9000 characters of PHP code before HTML HEADER.

Workaround: if possible move code after header or if not: include a file.

An Important Note about META tags and this function : if your META tag contains newline "\n" characters, get_meta_tags() will return a NULL value for that name property. Removing the newlines from the source META tag corrects the problem.

If the URL is doing a redirection using the headers (like you would do with PHP function header("Location: URL");), the page has no content (in general). It appears get_meta_tags() doesn't catch that kind of redirection (like cURL would do) and it lead me to a timeout of my script.

I experienced this in a spider I wrote in order to feed my database of all available pages on my site and one link was linking to a page that simply has the following code:

<?php header("Location: sections.php?section=home"); exit();?>

That made my script hang for a moment and apparently, get_meta_tags() wasn't even able to return me an error.

I have found that for large searches, get_meta_tags is very slow. I created a large search engine for a website that couldnt use a database and I first tried pulling out the meta tags.
I have found that it is actually much faster to use eregi to pull out the meta tags. This code below pulls out the description:

Something that is not mentioned above and should be : When using get_meta_tags on a remote PHP page the page will be parsed before the meta tags are returned - so you can capture meta tags generated dynamically (by PHP??) on the remote end.

This DOES NOT work the same way when getting meta tags on local file systems. Local files are not parsed through the web server before returning to get_meta_tags(). If the META tag is hard-coded into the page, you'll be fine - but if it dynamically generated you will not be able to capture it unless you use the full URL when calling your local files.

I used this as part of my mini php search based search engine - it really slowed the whole thing down. I wrote this function to read HTML (just fetch the file or use something like snoopy) and extract the meta data via a simple regex, works a treat and made my crawler much faster: