Home

See the related posts

Mysql server version using php function – mysql_get_server_info()    Text file prompt download or force download using php    Number of lines in a file using php    Check Image exists or Not Using php and GD    PHP Warning: Call-time pass-by-reference has been deprecated   

Get Title And Meta Keywords from Any Site With PHP

This code will help you to grab website title and meta keywords and meta description from any site.

<?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]*' .'[lang="]*[^>"]*["]*'.'[\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++)
                {
		    $metaname=strtolower($names[$i]);
			$metaname=str_replace("'",'',$metaname);
			$metaname=str_replace("/",'',$metaname);
                        $metaTags[$metaname] = array (
                        'html' => htmlentities($originals[$i]),
                        'value' => $values[$i]
                    );
                }
            }
        }
	if(sizeof($metaTags)==0) {
	preg_match_all('/<[\s]*meta[\s]*content="?' . '([^>"]*)"?[\s]*' .'[lang="]*[^>"]*["]*'.'[\s]*name="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);

	    if (isset($match) && is_array($match) && count($match) == 3)
        {
            $originals = $match[0];
            $names = $match[2];
            $values = $match[1];

            if (count($originals) == count($names) && count($names) == count($values))
            {
                $metaTags = array();

                for ($i=0, $limiti=count($names); $i < $limiti; $i++)
                {
		    $metaname=strtolower($names[$i]);
			$metaname=str_replace("'",'',$metaname);
			$metaname=str_replace("/",'',$metaname);
                    $metaTags[$metaname] = 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;
   //global $contents;
    $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;
}
?>
//------------------Usage--------------------------------
$Domain='http://www.samplephpcodes.com'; // website
$result = getUrlData($Domain);
if($result['title']=="") {
$title="No Data Available";
} else {
$title=$result['title'];
}
if($result['metaTags']['description']['value']=="") {
$description="No Data Available";
} else {
$description=$result['metaTags']['description']['value'];
}
if($result['metaTags']['keywords']['value']=="") {
$keywords="No Data Available";
} else {
$keywords=$result['metaTags']['keywords']['value'];
}
echo '<br>Title - '.$title;
echo '<br>Description - '.$description;
echo '<br>Keywords - '.$keywords;

Home

See the related posts

Mysql server version using php function – mysql_get_server_info()    Text file prompt download or force download using php    Number of lines in a file using php    Check Image exists or Not Using php and GD    PHP Warning: Call-time pass-by-reference has been deprecated   

1 Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment