Is this a good way to retrieve the title and description from the tags of an html page ?

<?php
require('../design/includes/application_top.php');

$banner_query = db_query("Select banners_id, banners_url from banners");
$num_rows = db_num_rows($banner_query);
$banners=db_fetch_array($banner_query);

$invalid_links="";
$invalid_error=false;

for ($i=0; $i<($num_rows); $i++){
	echo "<br>count: " . $i+1 . " --- url: " . $banners['banners_url'] ;
	$file =	file_get_contents($banners['banners_url']);

	updateRecord($i,$html);		
}

function updateRecord($i,$html){
echo"Count: " . $i+1 . "<br>" ;

	preg_match("'<title>.*?</title>'si",$html,$matches_title,1)
	preg_match("'<meta [^>]*?name=[^>]*?Description[^>]*? content=.*?>'si",$matches_description,$html,'1');

echo "Title:$matches_title[0]<br>Description=$matches_description[0]<br><br>"
}
?>

Example string to parse:
<html><head><title>Joes Site</title><meta name="description" content="joes really cool site"></head>

Thanks.

    Hello,

    Guys.. i tried the solution on that site and no luck..

    Can you please help me retrieve the title...

    updateRecord(1, "http://www.cnn.com");

    function updateRecord($i,$html){
    // does not work // preg_match("'<title>.?</title>'si",$html,$matches) ;
    // does not work // eregi("<title>(.
    )</title>", $html, $matches);
    // does not work // preg_match_all('/<title.>(.+)<\/title.>/uis', $html, $matches);

    	$tags = get_meta_tags($html);		
    	$title = $matches[0];
    	$description = $tags['description'];				
    
    	echo "title: $title<br>Description: $description<br><br>";
      function get_title($str)
      {
          if (($pos_1 = strpos($str, '<title>')) === false) {
              return false;
          }
          if (($pos_2 = strpos($str, '</title>', $pos_1 + 1)) === false) {
              return false;
          }
          return strip_tags(substr($str, $pos_1, $pos_2 - $pos_1));
      }

        i threw this together really quickly, seems to work...

        $html = file_get_contents("http://www.cnn.com/");
        
        $matched = preg_match("/<title[^>]*>(.*?)<\/title>/is", $html, $match);
        
        if($matched) {
          echo $match[1];
        } else {
          echo "No title tag found";
        }
        
        
          5 days later

          Hello,

          I tried the above solution but it doesn't work for all cases.
          for example, I used the above code:

          function getMetaTagsDescription($html){
          eregi('<META NAME="keywords" CONTENT="([\"])">', $html, $matches);
          return $matches[1];
          }
          function getTitle($html){
          // eregi("<title>(.
          )</title>", $html, $matches);
          }
          function get_title($str)
          {
          if (($pos_1 = strpos($str, '<title>')) === false) {
          return false;
          }
          if (($pos_2 = strpos($str, '</title>', $pos_1 + 1)) === false) {
          return false;
          }
          return strip_tags(substr($str, $pos_1, $pos_2 - $pos_1));
          }

          <title>Peet's Coffee &amp; Tea</title>

          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          <meta name="keywords" content="coffee, specialty, gourmet coffee, coffee beans,
          coffee blends, coffee gifts, tea gifts, tea, tea leaves and bags, herbal, green tea, black tea">

          and the title function (both from above) returned this: Peet's
          and the meta function returned just coffee, ...
          I dont' know why..
          thanks.

            i just tried my title function on that site, worked fine for me, even a little something to grab the description now that works..

            <?php
            $html = file_get_contents("http://www.peets.com/shop/shop.asp");
            
            $matched = preg_match("/<title[^>]*>(.*?)<\/title>/is", $html, $match);
            
            if($matched) {
              echo $match[1];
            } else {
              echo "No title tag found";
            }
            
            $matched = preg_match("/<meta name=.?keywords.? content=.?(.*?).?>/is", $html, $match);
            
            if($matched) {
              echo $match[1];
            } else {
              echo "No description";
            }
            
            ?>
            
            

              I tested every solution you've been given (putting each into a separate function), and they all work perfectly on both the cnn site and the example you gave.

              $strings[] = file_get_contents('http://www.cnn.com/');
              
              $strings[] = '<title>Peet\\'s Coffee & Tea</title>
              <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
              <meta name="keywords" content="coffee, specialty, gourmet coffee, coffee beans, 
              coffee blends, coffee gifts, tea gifts, tea, tea leaves and bags, herbal, green tea, black tea">';
              
              foreach ($strings as $string) {
                  echo getMetaTagsDescription($string) . '<br />';
                  echo get_the_title($string) . '<br />';
                  echo get_the_title_and_keywords($string) . '<br />';
                  echo getTitle($string) . '<br />';
                  echo get_title($string) . '<br />';
              }
              
              function getMetaTagsDescription($html)
              { 
                  eregi('<META NAME="keywords" CONTENT="([^\\"]*)">', $html, $matches); 
                  return $matches[1]; 
              } 
              
              function get_the_title($html)
              {
                  $matched = preg_match("/<title[^>]*>(.*?)<\/title>/is", $html, $match);
                  if($matched) {
                      return $match[1];
                  } else {
                      return "No title tag found";
                  }
              }
              
              function get_the_title_and_keywords($html)
              {
                  $matched = preg_match("/<title[^>]*>(.*?)<\/title>/is", $html, $match);
                  if($matched) {
                      $title = $match[1];
                  } else {
                      $title = "No title tag found";
                  }
                  $matched = preg_match("/<meta name=.?keywords.? content=.?(.*?).?>/is", $html, $match);
                  if($matched) {
                      $keywords = $match[1];
                  } else {
                      $keywords = "No description";
                  }
                  return $title . '<br />' . $keywords;
              }
              
              function getTitle($html) 
              { 
                   eregi("<title>(.*)</title>", $html, $matches);
                   return $matches[1]; 
              }
              
              function get_title($str) 
              { 
                  if (($pos_1 = strpos($str, '<title>')) === false) { 
                      return false; 
                  } 
                  if (($pos_2 = strpos($str, '</title>', $pos_1)) === false) { 
                      return false; 
                  } 
                  return strip_tags(substr($str, $pos_1, $pos_2 - $pos_1)); 
              } 
              
              /** Output:
              
              CNN.com
              CNN.com
              No description
              CNN.com
              CNN.com
              coffee, specialty, gourmet coffee, coffee beans, coffee blends, coffee gifts, tea gifts, tea, tea leaves and bags, herbal, green tea, black tea
              Peet's Coffee & Tea
              Peet's Coffee & Tea
              coffee, specialty, gourmet coffee, coffee beans, coffee blends, coffee gifts, tea gifts, tea, tea leaves and bags, herbal, green tea, black tea
              Peet's Coffee & Tea
              Peet's Coffee & Tea
              
              **/

                Hello,

                I want to apologize to the entire community for having to had wasted your time.. The solutions did work.. but my problem was the output... I was taking the output an putting it into a form element (<input type=text value=$getTitle>) and that was not displaying all of it.. I swithced over to another html form element Textarea and found that it works.. I leaned of my dumb mistake when i did a straight ouptput and it showed me that the error lay in the html form element.

                Once again, I apologize and am greateful for all your help.
                THanks.

                  14 days later

                  $matched = preg_match("/<applet[>]code=\"(.?).class[>]*>/is", $html, $match);

                  if($matched) {
                  echo $match[1].".class";
                  } else {
                  echo "No title tag found";
                  }

                  Is there a way for me to determine if the code= has something like "http://www.blach.com" ?

                  Thanks.

                    Write a Reply...