Thursday 13 December 2012

Get any Website Page Title From URL

This tutorial helps you to get the page title of any website via the website url.
The script of this tutorial takes the URL of a given page from which the page title is fetched.
To get started, just pass the url of the page that you wish to fetch its content to the getPage_Title_From_URL('url here') function and you are done.

code:
<?php
function getPage_Title_From_URL($pageURL)
{
      $readPG = fopen($pageURL'r');
      $x = 0;
      $specifiedPG = '';
      if ($readPG
      {
            while (!feof($readPG) && ($x < 10)) 
            {
                  $specifiedPG .= fread($readPG, 8192);
                  $x++;
            }
            fclose($readPG);
      }
      preg_match("/<title.*?>[\n\r\s]*(.*)[\n\r\s]*<\/title>/", $specifiedPG, $pageTitleFromURL);
      if (isset($pageTitleFromURL[1])) 
      {
            if ($pageTitleFromURL[1] == '') 
            {
                  return $pageURL;
            }
            $pageTitleFromURL = $pageTitleFromURL[1];
            return trim($pageTitleFromURL);
      } 
      else 
      {
            return $pageURL;
      }
}
//Example function call
echo getPage_Title_From_URL('https://www.dvlottery.state.gov/');
?>

No comments:

Post a Comment