Now in-order to grab the <meta> tags content attributes values from a web page or file you will need to use PHP's get_meta_tags() function which was introduced to us in PHP 4. The get_meta_tags() function extracts all the <meta> tags content attributes values from a file and returns it as an array. The get_meta_tags() function opens the filename parameter and parses it line by line for <meta> tags in the file or URL until it reaches the </head> end tag, in-order to stop parsing. Only <meta> tags with the name attribute will be parsed. The get_meta_tags() function returns an array with all the parsed <meta> tags.
The get_meta_tags() function turns the value of the <meta> tags name attribute into the key and the value of the content attribute becomes the value of the returned array, so you can easily use standard array functions to go through the values or access single values. Special characters like the question mark (?), period (.), plus (+), backslash (\), asterisk (*), bracket ([]), caret (^), space ( ), parenthesis (()), and dollar sign ($) that are in the value of the name attribute are substituted with the underscore (_) character, the rest is converted into lower case characters. Also if two <meta> tags have the same name, only the last one is returned.
The get_meta_tags() function has two parameters which I will list below:
- filename
-
The filename parameter sets the path to the HTML or XHTML file, as a string. The filename parameter can be a local file or a URL. Its important to pay attention to line endings in-other words newline characters like carriage returns and new line feeds for example, because PHP uses a native function to parse the input so older Mac files won't work on Unix and will have to be converted.
In PHP version 4.0.5 support for unquoted HTML and XHTML attributes was added. Else if the <meta> tags values associated with the name or content attributes are not quoted, the <meta> tags will not be parsed.
Now let me show you how to code the filename parameter.
<?php $file_name = get_meta_tags('http://www.example.com/'); echo $file_name['author']; echo $file_name['keywords']; echo $file_name['description']; echo $file_name['geo_region']; ?> - use_include_path
-
Now the use_include_path parameter is a boolean parameter which means its value can be either true or false. Setting the use_include_path parameter 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 only and not URLs.
Here is how to code the use_include_path parameter for the get_meta_tags() function.
<?php $file_name = get_meta_tags('C:/wamp/www/local-file.htm', true); echo $file_name['author']; echo $file_name['keywords']; echo $file_name['description']; echo $file_name['geo_region']; ?>
Now let me show you how to code the get_meta_tags() function with the filename parameter and then I will explain the code to you in more detail.
<?php
$file_name = get_meta_tags('http://www.example.com/');
echo $file_name['author'];
echo $file_name['keywords'];
echo $file_name['description'];
echo $file_name['geo_region'];
?>
Let's say that are filename parameter (http://www.example.com/) from the above code points to a URL that has the following <meta> tags below in the web page.
<meta name="author" content="Next Gen Web Design" /> <meta name="KEYWORDS" content="html, xhtml, html5, php, jquery, css" /> <meta name="description" content="Let us design your next web site today!" /> <meta name="geo.region" content="US-CA" />
Now remember that the get_meta_tags() function turns the value of the <meta> tags name attribute into the key and the value of the content attribute becomes the value of the returned array. The get_meta_tags() function creates the values from the <meta> tags, which are stored as an array in the variable $file_name for our example. So in-order to call the author <meta> tag from the array for example, all we have to do is specifying the variable $file_name and the desired key in lower case characters placed in brackets ([]) with or without quotes, but it's better to use quotes.
So in other words in-order to display the <meta> tags content attributes info in the above example from the get_meta_tags() function, which has converted the <meta> tags into an array our code will have to look something like the code below.
<?php $file_name = get_meta_tags('http://www.example.com/'); echo $file_name['author'] . '<br />'; echo $file_name['keywords'] . '<br />'; echo $file_name['description'] . '<br />'; echo $file_name['geo_region']; ?>
Now in the above code you can see how the values from the <meta> tags name attributes are changed from upper case characters to lower case characters and all the special characters are substituted with an underscore (_) character for the key values. This is how to code a key value.
The above code will display the following data/text below.
Next Gen Web Design html, xhtml, html5, php, jquery, css Let us design your next web site today! US-CA
If you want to see all the available <meta> tags with the name attribute from a file using the get_meta_tags() function just use the print_r function from the following code below, just remember to replace the filename parameter with your own file.
<?php
$file_name = get_meta_tags('http://www.example.com/');
echo '<pre>';
print_r($file_name);
echo '</pre>';
?>
The above code will display the following information below about the $file_name variable.
Array
(
[author] => Next Gen Web Design
[keywords] => html, xhtml, html5, php, jquery, css
[description] => Let us design your next web site today!
[geo_region] => US-CA
)
If you need more information about the get_meta_tags() function just check out the PHP manual at http://www.php.net/manual/en/function.get-meta-tags.php