Ciao,
ho saricato in rete un file per visualizzare i meta tag di un sito, tipo un meta tag analyzer,
stampa il risultato in Array, come faccio a recuperare ogni singolo tag dell'array e stamparlo:
Questo è il file on-line
http://www.vale2.org/metatag.php
Questo il .php
Come stampo i campi trovati nell'array separatamente.
Valeria.
ho saricato in rete un file per visualizzare i meta tag di un sito, tipo un meta tag analyzer,
stampa il risultato in Array, come faccio a recuperare ogni singolo tag dell'array e stamparlo:
Questo è il file on-line
http://www.vale2.org/metatag.php
Questo il .php
PHP:
<?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);
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;
}
$result = getUrlData('http://www.avg.com/it-it/homepage');
echo $result[title];
echo "<br>";
echo $result[metaTags][keywords][value];
echo '<pre>'; print_r($result); echo '</pre>';
//echo $result[metaTags][description][value];
?>
Codice:
Array
(
[title] => AVG | Antivirus e Internet Security | Protezione antivirus
[metaTags] => Array
(
[description] => Array
(
[html] => <meta name="description" content="AVG Technologies (NYSE:AVG) è leader globale nel settore del software di protezione. I suoi prodotti sono utilizzati da oltre 110 milioni di utenti consumer e piccole aziende."/>
[value] => AVG Technologies (NYSE:AVG) è leader globale nel settore del software di protezione. I suoi prodotti sono utilizzati da oltre 110 milioni di utenti consumer e piccole aziende.
)
[robots] => Array
(
[html] => <meta name="robots" content="index, follow" />
[value] => index, follow
)
..................
Come stampo i campi trovati nell'array separatamente.
Valeria.
Ultima modifica di un moderatore: