get_meta_tags

(PHP 4, PHP 5)

get_meta_tagsExtracts all meta tag content attributes from a file and returns an array

Description

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

Opens filename and parses it line by line for <meta> tags in the file. The parsing stops at </head>.

Parameters

filename

The path to the HTML file, as a string. This can be a local file or an URL.

Example #1 What get_meta_tags() parses

<meta name="author" content="name">
<meta name="keywords" content="php documentation">
<meta name="DESCRIPTION" content="a php manual">
<meta name="geo.position" content="49.33;-86.59">
</head> <!-- parsing stops here -->
(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.

Changelog

Version Description
4.0.5 Support for unquoted HTML attributes was added.

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/');

// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author'];       // name
echo $tags['keywords'];     // php documentation
echo $tags['description'];  // a php manual
echo $tags['geo_position']; // 49.33;-86.59
?>

Notes

Note:

Only meta tags with name attributes will be parsed.

See Also

Коментарии

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.
2000-04-21 20:00:49
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
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.
2000-04-21 20:17:15
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
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:

if (eregi ("<meta name=\"description\" content=[^>]*", $contents, $descresult))
                                {
                                    $description = explode("<meta name=\"description\" content=", $descresult[0]);
                                    echo "<font face=\"Arial\" size=2>$description[1]</font>";
                                   
                                }
2001-07-30 18:29:45
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
Автор:
Tested PHP 4.0.6

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.
2001-12-20 13:01:10
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
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.

JP.
2003-12-12 19:37:40
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
If you want to get the contents of tags other than meta you can use:

<?php

$page 
"http://www.mysite.com/apage.php";

   
// tags
   
$start '<atag>';
   
$end '<\/atag>';

   
// open the file
   
$fp fopen$page'r' );

   
$cont "";

   
// read the contents
   
while( !feof$fp ) ) {
       
$buf trimfgets$fp4096 ) );
       
$cont .= $buf;
    }
   
   
// get tag contents
   
preg_match"/$start(.*)$end/s"$cont$match );

   
// tag contents
   
$contents $match]; 

?>
2005-02-01 09:43:04
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
Автор:
in response to
jp at webgraphe dot com

this function grabs meta tags, not http headers

if you need the headers

<?php

$fp 
fopen('http://example.org/somepage.html''r');

// the variable $http_response_header magically appears
print_r($http_response_header);

// or
$meta_data stream_get_meta_data($fp);
print_r($meta_data);

?>
2005-02-05 20:45:19
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
Автор:
Tim's code is good (thanks Tim), except it won't work very well if the tag is part of a long non-breaking string.

E.g. try getting the title from Google Maps (http://www.google.com/maps).

A better solution is:

<?php 
$title 
"";
   
if (
$fp = @fopen$_POST['url'], 'r' )) {

   
$cont "";
   
   
// read the contents
   
while( !feof$fp ) ) {
       
$buf trim(fgets$fp4096 )) ;
       
$cont .= $buf;
    }

   
// get tag contents
   
@preg_match"/<title>([a-z 0-9]*)<\/title>/si"$cont$match );
   
   
// tag contents
   
$title strip_tags(@$match]); 


?>

Note the strip_tags. Another thing to be careful of is to check for ", <, and >. You will need to strip those out if you are posting the output to a form.

Also, it is probably best to use the /i modifier, because some people might code <TITLE> etc...
2005-03-12 02:47:45
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
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 ;)

<?php
function getUrlData($url)
{
   
$result false;
   
   
$contents getUrlContents($url);

    if (isset(
$contents) && is_string($contents))
    {
       
$title null;
       
$metaTags null;
       
       
preg_match('/<title>([^>]*)<\/title>/si'$contents$match );

        if (isset(
$match) && is_array($match) && count($match) > 0)
        {
           
$title strip_tags($match[1]);
        }
       
       
preg_match_all('/<[\s]*meta[\s]*name="?' '([^>"]*)"?[\s]*' 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si'$contents$match);
       
        if (isset(
$match) && is_array($match) && count($match) == 3)
        {
           
$originals $match[0];
           
$names $match[1];
           
$values $match[2];
           
            if (
count($originals) == count($names) && count($names) == count($values))
            {
               
$metaTags = array();
               
                for (
$i=0$limiti=count($names); $i $limiti$i++)
                {
                   
$metaTags[$names[$i]] = array (
                       
'html' => htmlentities($originals[$i]),
                       
'value' => $values[$i]
                    );
                }
            }
        }
       
       
$result = array (
           
'title' => $title,
           
'metaTags' => $metaTags
       
);
    }
   
    return 
$result;
}

function 
getUrlContents($url$maximumRedirections null$currentRedirection 0)
{
   
$result false;
   
   
$contents = @file_get_contents($url);
   
   
// Check if we need to go somewhere else
   
   
if (isset($contents) && is_string($contents))
    {
       
preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' '[\s]*[\/]?[\s]*>/si'$contents$match);
       
        if (isset(
$match) && is_array($match) && count($match) == && count($match[1]) == 1)
        {
            if (!isset(
$maximumRedirections) || $currentRedirection $maximumRedirections)
            {
                return 
getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
            }
           
           
$result false;
        }
        else
        {
           
$result $contents;
        }
    }
   
    return 
$contents;
}
?>

Here's an example of its usage. Check that the included URL has a META REFRESH redirection:

<?php
$result 
getUrlData('http://www.marianoiglesias.com.ar/');

echo 
'<pre>'print_r($result); echo '</pre>';

?>

For the above code the output would be:

<?php
Array
(
    [
title] => Mariano IglesiasEl Eternauta   
   
[metaTags] => Array
        (
            [
description] => Array
                (
                    [
html] => <meta name="description" content="Java, PHP, and some other technological mumble jumble. Also, some real-life stuff as well." />
                    [
value] => JavaPHP, and some other technological mumble jumbleAlsosome real-life stuff as well.
                )

            [
DC.title] => Array
                (
                    [
html] => <meta name="DC.title" content="Mariano Iglesias - Weblog" />
                    [
value] => Mariano Iglesias Weblog
               
)

            [
ICBM] => Array
                (
                    [
html] => <meta name="ICBM" content="-34.6017, -58.3956" />
                    [
value] => -34.6017, -58.3956
               
)

            [
geo.position] => Array
                (
                    [
html] => <meta name="geo.position" content="-34.6017;-58.3956" />
                    [
value] => -34.6017;-58.3956
               
)

            [
geo.region] => Array
                (
                    [
html] => <meta name="geo.region" content="AR-BA">
                    [
value] => AR-BA
               
)

            [
geo.placename] => Array
                (
                    [
html] => <meta name="geo.placename" content="Buenos Aires">
                    [
value] => Buenos Aires
               
)

        )

)
?>
2005-09-12 20:18:16
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
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:

<?php

function get_meta_data($html) {

   
preg_match_all(
   
"|<meta[^>]+name=\\"([^\\"]*)\\"[^>]+content=\\"([^\\"]*)\\"[^>]+>|i"$html$out,PREG_PATTERN_ORDER);

    for (
$i=0;$i count($out[1]);$i++) {
       
// loop through the meta data - add your own tags here if you need
       
if (strtolower($out[1][$i]) == "keywords"$meta['keywords'] = $out[2][$i];
        if (
strtolower($out[1][$i]) == "description"$meta['description'] = $out[2][$i];
    }

return 
$meta;   
}

?>
2005-09-20 15:37:15
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
This is a slight amendment to jimmyxx at gmail dot com function

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)
<?php
preg_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
2006-08-19 06:33:55
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
Quick meta data grabber
[code]
if(get_meta_tags('http://'.$_POST['pagina'])){
        print '<font class="midden">Meta data from http://'.$_POST['pagina'].'</font>';
        $metadata = get_meta_tags('http://'.$_POST['pagina']);
        echo '<table width="100%">';
        print '<tr><td>Meta</td><td>Waarde</td></tr>';
        foreach($metadata as $naam => $waarde){
            echo '<tr><td valign="top">'.$naam.'</td><td>'.$waarde.'</td></tr>';
        }
        print '</table>';
    }else{
        print '
        <div class="red_h">Incorrect</div>
        ';
    }
[/code]
2008-01-16 12:38:36
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
<?php

/*
** Extracts and formats meta tag content 
*/

function get_meta_data($url$searchkey='') {   
   
$data get_meta_tags($url);    // get the meta data in an array
   
foreach($data as $key => $value) {
        if(
mb_detect_encoding($value'UTF-8, ISO-8859-1'true) != 'ISO-8859-1') {    // check whether the content is UTF-8 or ISO-8859-1
           
$value utf8_decode($value);    // if UTF-8 decode it
       
}
       
$value strtr($valueget_html_translation_table(HTML_ENTITIES));    // mask the content
       
if($searchkey != '') {    // if only one meta tag is in demand e.g. 'description'
           
if($key == $searchkey) {
               
$str $value;    // just return the value
           
}
        } else {   
// all meta tags
           
$pattern '/ |,/i';    // ' ' or ','
           
$array preg_split($pattern$value, -1PREG_SPLIT_NO_EMPTY);    // split it in an array, so we have the count of words           
           
$str .= '<p><span style="display:block;color:#000000;font-weight:bold;">' $key ' <span style="font-weight:normal;">(' count($array) . ' words | ' strlen($value) . ' chars)</span></span>' $value '</p>';    // format data with count of words and chars
       
}
    }
    return 
$str;
}

$content .= get_meta_data("http://www.example.com/");
/*
output looks like this:

description (23 words | 167 chars)
SELFHTML 8.1.2 - Die bekannte Dokumentation zu HTML, JavaScript und CGI/Perl - Tutorial und Referenz, mit etlichen Zusatztips zu Design, Grafik, Projektverwaltung usw.

keywords (13 words | 119 chars)
SELFHTML, HTML, Dynamic HTML, JavaScript, CGI, Perl, Grafik, WWW-Seiten, Web-Seiten, Hilfe, Dokumentation, Beschreibung

etc.

*/

$content .= get_meta_data("http://www.example.com/""description");
/*
output looks like this:

SELFHTML 8.1.2 - Die bekannte Dokumentation zu HTML, JavaScript und CGI/Perl - Tutorial und Referenz, mit etlichen Zusatztips zu Design, Grafik, Projektverwaltung usw.
*/

?>
2008-11-27 06:12:05
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
Автор:
It's not work if meta syntax not have trailing slash.
2009-02-10 16:21:33
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
this function could get each meta of html content , and stripped all js and css. 

<?php
function get_meta_data($content)
{
   
$content strtolower($content);
   
$content preg_replace("'<style[^>]*>.*</style>'siU",'',$content);  // strip js
   
$content preg_replace("'<script[^>]*>.*</script>'siU",'',$content); // strip css
   
$split explode("\n",$content);
    foreach (
$split as $k => $v)
    {
        if (
strpos(' '.$v,'<meta')) {
             
preg_match_all(
"/<meta[^>]+(http\-equiv|name)=\"([^\"]*)\"[^>]" "+content=\"([^\"]*)\"[^>]*>/i",
$v$split_content[],PREG_PATTERN_ORDER);;
        }
    }
    return 
$split_content;
}
?>
2009-07-14 00:36:28
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
Автор:
Be aware that the function looks for the metatags in the whole page. If one of the meta is commented in your code for some reason, it will still be grabed.
2013-03-08 20:31:06
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
I personally experienced less issues using the DOM functions than regular expressions while trying to fetch meta tags and not using get_meta_tags function (in order to get http-equiv meta tags too).

<?php

$doc 
= new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXPath($doc);

$nodes $xpath->query('//head/meta');

foreach(
$nodes as $node) {
    [...]
}

?>
2013-10-02 23:50:23
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
Автор:
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.

function getUrlData($url, $raw=false) // $raw - enable for raw display
{
    $result = false;
   
    $contents = getUrlContents($url);

    if (isset($contents) && is_string($contents))
    {
        $title = null;
        $metaTags = null;
        $metaProperties = null;
       
        preg_match('/<title>([^>]*)<\/title>/si', $contents, $match );

        if (isset($match) && is_array($match) && count($match) > 0)
        {
            $title = strip_tags($match[1]);
        }
       
        preg_match_all('/<[\s]*meta[\s]*(name|property)="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);
       
        if (isset($match) && is_array($match) && count($match) == 4)
        {
            $originals = $match[0];
            $names = $match[2];
            $values = $match[3];
           
            if (count($originals) == count($names) && count($names) == count($values))
            {
                $metaTags = array();
                $metaProperties = $metaTags;
                if ($raw) {
                    if (version_compare(PHP_VERSION, '5.4.0') == -1)
                         $flags = ENT_COMPAT;
                    else
                         $flags = ENT_COMPAT | ENT_HTML401;
                }
               
                for ($i=0, $limiti=count($names); $i < $limiti; $i++)
                {
                    if ($match[1][$i] == 'name')
                         $meta_type = 'metaTags';
                    else
                         $meta_type = 'metaProperties';
                    if ($raw)
                        ${$meta_type}[$names[$i]] = array (
                            'html' => htmlentities($originals[$i], $flags, 'UTF-8'),
                            'value' => $values[$i]
                        );
                    else
                        ${$meta_type}[$names[$i]] = array (
                            'html' => $originals[$i],
                            'value' => $values[$i]
                        );
                }
            }
        }
       
        $result = array (
            'title' => $title,
            'metaTags' => $metaTags,
            'metaProperties' => $metaProperties,
        );
    }
   
    return $result;
}

function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
{
    $result = false;
   
    $contents = @file_get_contents($url);
   
    // Check if we need to go somewhere else
   
    if (isset($contents) && is_string($contents))
    {
        preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $contents, $match);
       
        if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1)
        {
            if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections)
            {
                return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
            }
           
            $result = false;
        }
        else
        {
            $result = $contents;
        }
    }
   
    return $contents;
}
?>

<?php
$result 
getUrlData('http://whatever...'true);

echo 
'<pre>'print_r($resulttrue); echo '</pre>';

?>

Output example:

<?php
Array
(
    [
title] => The requested page's title
    [metaTags] => Array
        (
            [description] => Array
                (
                    [html] => <meta name="description" content="Something..." />
                    [value] => Something...
                )
        )
    [metaProperties] => Array
        (
            [og:type] => Array
                (
                    [html] => <meta property="og:type" content="article"/>/>
                    [value] => article
                )
        )
)
?>
2015-04-25 23:38:07
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html
Автор:
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

<?PHP

function getMetaTags($str)
{
 
$pattern '
  ~<\s*meta\s

  # using lookahead to capture type to $1
    (?=[^>]*?
    \b(?:name|property|http-equiv)\s*=\s*
    (?|"\s*([^"]*?)\s*"|\'\s*([^\']*?)\s*\'|
    ([^"\'>]*?)(?=\s*/?\s*>|\s\w+\s*=))
  )

  # capture content to $2
  [^>]*?\bcontent\s*=\s*
    (?|"\s*([^"]*?)\s*"|\'\s*([^\']*?)\s*\'|
    ([^"\'>]*?)(?=\s*/?\s*>|\s\w+\s*=))
  [^>]*>

  ~ix'
;
 
  if(
preg_match_all($pattern$str$out))
    return 
array_combine($out[1], $out[2]);
  return array();
}

// usage
$meta_tags getMetaTags($str);

?>
2015-08-05 23:07:29
http://php5.kiev.ua/manual/ru/function.get-meta-tags.html

    Поддержать сайт на родительском проекте КГБ