Jump to content

Using PHP To Get A Site title and meta description from a url


Brandon_R

Recommended Posts

you can use file_get_contents("http://yoursite.com");

 

here is the title function

function getMetaTitle($content){
$pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";
if(preg_match($pattern, $content, $match))
return $match[1];
else
return false;
}

 

and its easy to get meta tags use this:

$blah=get_meta_tags("http://yoursite.com");

 

hope this helps.

<?php
function getMetaDescription($content) {
    $metaDescription = false;
    $metaDescriptionPatterns = array("/]*>/Ui", "/]*>/Ui");
    foreach ($metaDescriptionPatterns as $pattern) {
        if (preg_match($pattern, $content, $match))
             $metaDescription = $match[1];
             break;
    }
    return $metaDescription;
}
?>

<?php
$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
?>

 

this was collected from php.net hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.