Printing array values | Meta Tag Analyzer
I have an array that I would like to print out.
I have a test form up here that will analyze a given site's meta tags. I would like to simply get the meta-tags, and then list them or put them into a table...
So far, here is what I have:
http://metataggenerator.org/analyzer/
You can enter an URL into the box and the script will print out the meta tags in a list or table.
The code thus far:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Meta Tag Analyzer</title>
</head>
<body>
<form action="test.php" method="post">
<input type="text" name="url" onFocus="this.value=''; this.onfocus=null;" onblur="this.value='Enter your URL here';" value="Enter your URL here" />
<input type="submit" value="Analyze Meta Tags" />
</form><br />
<?php
$url = $_POST['url'];
$url = str_replace("Enter your URL here", '', $url); //makes sure that there is an URL
if ($url == '' ) {
echo "Enter an URL to analyze above";
echo "\n";
} else {
$url = "http://$url"; // adds http:// to the URL in case the user forgets it...
$url = preg_replace('[http://http://]', 'http://', $url); // makes the form work with or without the http:// by simply removing double http://http://
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); // Check if we need to go somewhere else
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("$url");
echo '<hr />';
echo '<h1>Results</h1>';
echo "<b>Results for:</b> ";
echo '<font color="red" face="courier">';
echo $url;
echo '</font>';
echo '<br />';
echo "<title>";
echo $result[title];
echo '</title>';
echo '<br /><font size="-1">';
echo $result[metaTags][description][html];
echo '<br />';
echo $result[metaTags][keywords][html];
echo '<br />';
echo $result[metaTags][Charset][html];
echo '<br />';
echo '<font color="red" face="arial">↑ <i>This is what I would like the results page to look like, except with the full listing of all metatags on the page</i></font>';
echo '<hr />';
echo 'Here is a listing of the types of tags that appear on the page, but I cannot figure out how to extract the HTML array and display it...';
// start table and print heading
reset($result[metaTags]);
list($c1, $c2) = each($result[metaTags]);
echo("<table><tr><td>$c1</td><td>$c2</td></tr>\n");
// print the rest of the values
while (list($c1,$c2) = each($result[metaTags])) {
echo("<tr><td>$c1</td><td>$c2</td></tr>\n");
}
// end the table
echo("</table>");
//end test
echo '<hr />';
echo '</font>';
/////////////////
echo '<pre>';
print_r($result[metaTags]);
echo '</pre>';
}
?>
</body>
</html>