Code / PHP Snippets / Function: get_html_title()

  1. <?
  2. // Function grabs HTML page, tries to extract HTML <title>
  3. // element and returns contents, or URL if not title found.
  4. function get_html_title($url) {
  5.         $page = file_get_contents($url);
  6.         preg_match("/<title>(.*)</title>/",$page,$matches);
  7.         $title = $matches[1];
  8.         if ($title) {
  9.                 return $title;
  10.         } else {
  11.                 return $url;
  12.         }
  13. }
  14. ?>

Download as Text : January 6th, 2009