6

I am having trouble getting the meta description/title from this specific site.

Here is some code:

$file = file('http://www.thegooddrugsguide.com/lsd/index.htm');
$file = implode("",$file);
if (preg_match('/<title>(.*?)<\/title>/is',$file,$t)) $title = $t[1];

It works with other sites, but not with the site in question. What could be the problem?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
johnny
  • 61
  • 1
  • 1
  • 2

2 Answers2

20

This should work fine:

$doc = new DOMDocument;
$doc->loadHTMLFile('http://example.com');

$title = $doc->getElementsByTagName('title');
$title = $title[0];

$metas = $doc->getElementsByTagName('meta');

foreach ($metas as $meta) {
  if (strtolower($meta->getAttribute('name')) == 'description') {
    $description = $meta->getAttribute('value');
  }
}

More info: http://www.php.net/manual/en/book.dom.php

Edit: this shorter version can also work to find the description:

$xpath = new DOMXPath($doc);
$description = $xpath->query('//meta[@name="description"]/@content');
seriousdev
  • 7,519
  • 8
  • 45
  • 52
  • Hey I am getting this error: `DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: Invalid char in CDATA 0x1F in http://www.thegooddrugsguide.com/lsd/index.htm, line: 1` – johnny May 25 '11 at 02:02
  • Also I have tried example.com and then I get this error: `Cannot use object of type DOMNodeList as array ` – johnny May 25 '11 at 02:06
  • @johnny just edited adding ->item(0)->nodeValue; instead of [0]. Just tested and it works now – Baronth Nov 14 '12 at 11:27
  • @Baronth : what are you talking about? I am getting the same error 'Cannot use object of type DOMNodeList as array', can you please mention the change in seriousdev's answer? – Code Dec 04 '12 at 05:26
  • 1
    Someone modified it again(or the modify wasn't approved)... I'll write it here: Instead of `$title = $title[0];` use `$title = $title->item(0)->nodeValue;` – Baronth Dec 04 '12 at 13:54
  • Note that the Edit contains an error. `$description = $xpath->query('/html/head/meta[name@="description"]/@content');` should read `$description = $xpath->query('/html/head/meta[@name="description"]/@content');` The `@` should come before the name attribute selector not afterwards. – olliefinn Aug 08 '13 at 09:53
  • 1
    you should do `$description = $meta->getAttribute('content');` instead of `getAttribute('value')` – Michael Konečný Nov 17 '15 at 17:48
3
$url = "http://www.thegooddrugsguide.com/lsd/index.htm";    
$tags = get_meta_tags($url);
$description = $tags["description"];
AlBeebe
  • 8,101
  • 3
  • 51
  • 65