Get meta content php

get_meta_tags

Открывает filename и разбирает его строка за строкой в поисках тегов . Разбор файла останавливается на теге .

Список параметров

Путь к HTML файлу, строка. Может быть как локальным файлом, так и URL .

Пример #1 Что обрабатывает функция get_meta_tags()

(обратите внимание на символы конца строки — PHP использует для разбора строк функции, встроенные в операционную систему, поэтому файлы, созданные в MacOS не будут правильно обрабатываться на Unix).

Если use_include_path равен TRUE , PHP будет искать файл используя стандартные пути поиска из директивы php.ini include_path. Это актуально только для локальных файлов, но не для URL.

Возвращаемые значения

Возвращает ассоциативный массив со значениями разобранных мета-тегов.

Значение атрибута name становится ключом массива, а значение атрибута content — значением этого элемента. Вы можете использовать стандартные функции работы с массивами для обхода или доступа к конкретным значениям. Специальные символы в именах (ключах массива) заменяются на ‘_’, и ключи приводятся к нижнему регистру. Если два мета-тега имеют одинаковые имена, будет возвращен только последний.

Примеры

Пример #2 Что возвращает функция get_meta_tags()

// Предположим, что указанные выше мета-теги расположены на www.example.com
$tags = get_meta_tags ( ‘http://www.example.com/’ );

// Обратите внимание, что ключи приведены к нижнему регистру, а
// точки (‘.’) в ключах заменены на ‘_’
echo $tags [ ‘author’ ]; // name
echo $tags [ ‘keywords’ ]; // php documentation
echo $tags [ ‘description’ ]; // a php manual
echo $tags [ ‘geo_position’ ]; // 49.33;-86.59
?>

Примечания

Замечание:

Обрабатываются только мета-теги с атрибутом name. Кавычки не требуются.

Смотрите также

  • htmlentities() — Преобразует все возможные символы в соответствующие HTML-сущности
  • urlencode() — URL-кодирование строки

Источник

get_meta_tags

Opens filename and parses it line by line for tags in the file. The parsing stops at .

Parameters

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

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.

Returns false on failure.

Examples

Example #2 What get_meta_tags() returns

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

Читайте также:  Python uno install windows

Notes

Note:

Only meta tags with name attributes will be parsed. Quotes are not required.

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

if( preg_match_all ( $pattern , $str , $out ))
return array_combine ( $out [ 1 ], $out [ 2 ]);
return array();
>

// usage
$meta_tags = getMetaTags ( $str );

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

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

$nodes = $xpath -> query ( ‘//head/meta’ );

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:

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.

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.

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.

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;

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)
$[$names[$i]] = array (
‘html’ => htmlentities($originals[$i], $flags, ‘UTF-8’),
‘value’ => $values[$i]
);
else
$[$names[$i]] = array (
‘html’ => $originals[$i],
‘value’ => $values[$i]
);
>
>
>

Читайте также:  Javascript show html content

$result = array (
‘title’ => $title,
‘metaTags’ => $metaTags,
‘metaProperties’ => $metaProperties,
);
>

function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
$result = false;

// Check if we need to go somewhere else

echo ‘

' ; print_r ( $result , true ); echo '

‘ ;

in response to
jp at webgraphe dot com

this function grabs meta tags, not http headers

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

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

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 😉

function getUrlData ( $url )
$result = false ;

$contents = getUrlContents ( $url );

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

function getUrlContents ( $url , $maximumRedirections = null , $currentRedirection = 0 )
$result = false ;

$contents = @ file_get_contents ( $url );

// Check if we need to go somewhere else

return $contents ;
>
?>

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

echo ‘

' ; print_r ( $result ); echo '

‘ ;

?>

For the above code the output would be:

Array
(
[ title ] => Mariano Iglesias : El 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 ] => Java , PHP , and some other technological mumble jumble . Also , some real — life stuff as well .
)

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)
preg_match_all (
«|]+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

If you want to get the contents of tags other than meta you can use:

// open the file
$fp = fopen ( $page , ‘r’ );

// 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 ];

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

if ( $fp = @ fopen ( $_POST [ ‘url’ ], ‘r’ ))

// read the contents
while( ! feof ( $fp ) ) $buf = trim ( fgets ( $fp , 4096 )) ;
$cont .= $buf ;
>

// get tag contents
@ preg_match ( «/([a-z 0-9]*)/si» , $cont , $match );

// tag contents
$title = strip_tags (@ $match [ 1 ]);
>

?>

Note the strip_tags. Another thing to be careful of is to check for «, . 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 etc.

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.

/*
** 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 .= ‘

‘ . $key . ‘ (‘ . count ( $array ) . ‘ words | ‘ . strlen ( $value ) . ‘ chars)‘ . $value . ‘

‘ ; // 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

$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.
*/

Источник

Оцените статью