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(pay attention to line endings - PHP uses a native function to parse the input, so a Mac file won't work on Unix).
<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 -->
use_include_path- Setting
use_include_pathtoTRUEwill 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 #1 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']; // nameecho $tags['keywords']; // php documentationecho $tags['description']; // a php manualecho $tags['geo_position']; // 49.33;-86.59?>
Example #2 What get_meta_tags() returns
<?phpfunction 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;
}?>
Example #3 What get_meta_tags() returns
<?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($value, get_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, -1, PREG_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");
?>






0 comments:
Post a Comment