How to fetch the meta details of a site???

Discussion in 'PHP' started by vetrivel, May 6, 2009.

  1. #1
    Hi ,
    I would like to fetch the meta decription and keyword of many sites .I have a script to do it.While doing so
    I have come across three different meta tag structure :
    1. <meta xmlns="http://wwww.w3.org/1999/xhtml" name="description" content="meta decription goes here" />
    2.<meta name="description" content="meta decription goes here" />
    3.<meta http-equiv="description" content="meta decription goes here" />

    How to fetch the description.Due to this difference in structure i am unable to fetch the details and as well us i need to know is there any other type other than the above three.
    FYI:Same goes to meta keywords.

    Have anybody faced this problem?
    Do you have any solution or suggestion.
    Please advice me.
    Cheers.
     
    vetrivel, May 6, 2009 IP
  2. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is the PHP code i made for you:
    
    <?php
    $site=file_get_contents("http://aioshared.com");
    $head=explode("<head>",$site);
    $head=explode("</head>",$head[1]);
    $head=$head[0];
    $desc=explode('name="description"',$head);
    $desc=explode("content=",$desc[1]);
    $desc=explode(">",$desc[1]);
    $arr=array('"','/');
    $description=str_ireplace($arr,"",$desc[0]);
    if ($description==""){
    $description="Can't Fetch Description";
    }
    
    $keyw=explode('name="keywords"',$head);
    $keyw=explode("content=",$keyw[1]);
    $keyw=explode(">",$keyw[1]);
    $arr=array('"','/');
    $keywords=str_ireplace($arr,"",$keyw[0]);
    if ($keywords==""){
    $keywords="Can't Fetch Keywords";
    }
    
    echo "<b>Description:</b>".$description."<br>";
    echo "<b>Keywords:</b>".$keywords."<br>";
    ?>
    
    PHP:
    If you face any problem with it, then let me know :)

    Thanks!
     
    mehdi, May 6, 2009 IP
  3. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    #3 is just bad html and I don't think you'll come across it too often. For the rest, this should work:

    $dom = new DOMDocument();
    
      @$dom->loadHTML($html);
            
      $metas = $dom -> getElementsByTagName('meta');
    
      foreach ($metas as $meta) {
          if ( $meta ->getAttribute('name') == 'description') {
          echo $meta->getAttribute('content');
          }       
        }
    PHP:
    Where $html is a string with your html data.
     
    JDevereux, May 6, 2009 IP
  4. dzysyak

    dzysyak Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How about the following:
    
    // Assuming the above tags are at www.example.com
    $tags = get_meta_tags('http://www.example.com/');
    
    // Notice how the keys are all lowercase now, and
    // how . was replaced by _ in the key.
    echo $tags['author'];       // name
    echo $tags['keywords'];     // php documentation
    echo $tags['description'];  // a php manual
    echo $tags['geo_position']; // 49.33;-86.59
    
    
    
    PHP:
     
    dzysyak, May 20, 2009 IP