Hi Guys
I noticed that the metaengine() below sample.php returns "Not Available" if it can't find a meta tag.
I would prefer if the script did not update the text database if this happens and perhaps redirected to
a manual input form instead. I've had a go but my sample script below I wrote is not working properly
but I think it's nearly right. Could you please help me to fix this as I'm new to php and am a little out of
my depth.
Thanks again
Greg๐
<?php //sample.php
// Include the function so you can use it later in the page
require('metaengine.php');
// Call the MetaEngine function and assign the return value to an array
$metaInfo = MetaEngine($url);
// Text database
$data_file = "data.txt";
if (strlen($metaInfo[title]) && strlen($metaInfo[description]) && strlen($metaInfo[keywords]) {
$line = "2001----html----$metaInfo[title]----$metaInfo[description]----$url----$metaInfo[keywords]----(none given)\n";
$fd = fopen($data_file, "a") or die("Could not open data file!");;
fputs($fd, $line);
fclose($fd);
}
else {
/Display form with title, description, keywords. Then post information back this page and check again to see if all of them exist.
If you do that you will also need to have a input type=\"hidden\" hidden form field that
indicates that you have already checked the url for the meta info. Make sure you assign
the variable to the value of the form text/
//field as in:
echo "<input type=\"text\" size=\"20\" name=\"url\" value=\"$metaInfo[title]\"> ";
}
echo "
<head>
<title>Sample</title>
</head>
<body>
<form method=\"POST\" action=\"sample.php\">
<p align=\"center\"><font face=\"Arial\" size=\"2\"><b>Enter URL:</b></font> <input type=\"text\" name=\"url\" size=\"25\"></p>
<p align=\"center\"><input type=\"submit\" value=\"Submit\" name=\"s\"></p>
</form>
<p align=\"left\"><font size=\"2\" face=\"Arial\"><b><u>URL:</u></b> $url </font></p>
<p align=\"left\"><font size=\"2\" face=\"Arial\"><b><u>Title:</u></b> $metaInfo[title] </font></p>
<p align=\"left\"><b><u><font size=\"2\" face=\"Arial\">Description:</u></b> $metaInfo[description] </font></p>
<p align=\"left\"><font size=\"2\" face=\"Arial\"><b><u>Keywords:</u></b> $metaInfo[keywords] </font></p>
</body>
";
?>
<?php // metaengine.php
function MetaEngine($url)
{
// Pattern for meta title
$p_title[0] = '(<title>)([a-zA-Z_0-9@!%-;&,\'\+\$\?\.\n\t\r ]+)';
$p_title[1] = '(<meta[[:space:]]+name[ \'\"]*=[ \'\"]*title[ \'\"]*[[:space:]]*content[ \'\"]*=[ \'\"]*)([a-zA-Z_0-9@!%-;&,\'+\$\?.\n\t\r ]+)';
// Pattern for meta description
$p_description[0] = '(<meta[[:space:]]+name[ \'\"]=[ \'\"]description[ \'\"][[:space:]]content[ \'\"]=[ \'\"])([a-zA-Z_0-9@!%-;&`,\'+\$\?.\n\t\r ]+)';
// Pattern for meta keywords
$p_keywords[0] = '(<meta[[:space:]]+name[ \'\"]=[ \'\"]keywords[ \'\"][[:space:]]content[ \'\"]=[ \'\"])([a-zA-Z_0-9@!%-;&`,\'+\$\?.\n\t\r ]+)';
$p_keywords[1] = '(</head>)(.+)';
// Fetch file into an array
if(!($file = @file( $url, "r" )))
{
$keywords = 'Not Available';
$description = 'Not Available';
$title = 'Not Available';
}
else
{
// Turn array into a string using a space as the delimiter.
$target = @implode( " ", $file);
// Remove tab, return, and newline characters.
$pat = "\n";
$repl = " ";
$target = ereg_replace($pat, $repl, $target);
$pat = "\t";
$repl = " ";
$target = ereg_replace($pat, $repl, $target);
$pat = "\r";
$repl = " ";
$target = ereg_replace($pat, $repl, $target);
// Evaluate string with regular expression and find match for title.
if(eregi($p_title[0], $target, $match))
{
$title = $match[2];
}
elseif(eregi($p_title[1], $target, $match))
{
$title = $match[2];
}
else
{
$title = 'Not Available';
}
// Evaluate string with regular expression and find match for description.
if(eregi($p_description[0], $target, $match))
{
$description = $match[2];
}
else
{
$description = 'Not Available';
}
// Evaluate string with regular expression and find match for keywords.
if(eregi($p_keywords[0], $target, $match))
{
$keywords = $match[2];
}
// If no meta tag content is presend for keywords use document text as keywords
// starting after the </head> tag.
elseif(eregi($p_keywords[1], $target, $match))
{
//Remove HTML and PHP tags
$match[2] = strip_tags($match[2]);
//Strip white spaces before and after string
$match[2] = trim($match[2]);
//Limit size of string to 300 characters starting at the 100th character
$match[2] = substr($match[2], 100, 400);
$keywords = trim($match[2]);
}
else
{
$keywords = 'Not Available';
}
}
$metatag[title] = $title;
$metatag[description] = $description;
$metatag[keywords] = $keywords;
return $metatag;
}
?>