search for in the  
<get_headershttp_build_query>
Last updated: Thu, 19 May 2005

get_meta_tags

(PHP 3 >= 3.0.4, PHP 4, PHP 5)

get_meta_tags --  Extracts all meta tag content attributes from a file and returns an array

Description

array get_meta_tags ( string filename [, bool use_include_path] )

Opens filename and parses it line by line for <meta> tags in the file. This can be a local file or an URL. The parsing stops at </head>.

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.

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).

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.

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
?>

Note: As of PHP 4.0.5, get_meta_tags() supports unquoted HTML attributes.

See also htmlentities() and urlencode().



User Contributed Notes
get_meta_tags
Michael Knapp
12-Mar-2005 02:47
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( $fp, 4096 )) ;
      
$cont .= $buf;
   }

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

?>

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...
rehfeld
05-Feb-2005 08:45
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);

?>
tim dot bennett at haveaniceplay dot com
01-Feb-2005 09:43
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 = trim( fgets( $fp, 4096 ) );
      
$cont .= $buf;
   }
  
  
// get tag contents
  
preg_match( "/$start(.*)$end/s", $cont, $match );

  
// tag contents
  
$contents = $match[ 1 ];

?>
jp at webgraphe dot com
12-Dec-2003 07:37
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.
bill.neumann at hatworld.com
13-Mar-2003 09:50
The get_meta_tags function does not seem to be able to grab values if there are spaces between the attribute, the equal sign, and the opening quote marks.
20-Dec-2001 01:01
Tested PHP 4.0.6<br>
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 ...<br>
Tested using get_meta_tags() on local files with about 9000 characters of PHP code before HTML HEADER.<br>
Workaround: if possible move code after header or if not: include a file.
Ben dot Davis at furman dot edu
30-Jul-2001 05:29
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>";
                                  
                               }
richard at pifmagazine dot com
21-Apr-2000 07:17
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.
<p>
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.
richard at pifmagazine dot com
21-Apr-2000 07:00
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.

<get_headershttp_build_query>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT