Mike
Mike

Reputation: 980

getting title of page using simple html dom?

I'm trying to get the title from an external site using simple html dom (the title of page between the TITLE tags) but it's not retrieving anything. Any ideas?

$html = new simple_html_dom();
$html->load('http://www.google.com');
$titleraw = $html->find('title');
$title = $titleraw->title;

Upvotes: 0

Views: 8701

Answers (8)

dmani ms
dmani ms

Reputation: 44

Use this

$html = new simple_html_dom();  
$html->load('http://www.google.com');
$titleraw = $html->find('title');
foreach($html->find('title') as $link_element) {        
    echo $link_element->plaintext;
}

instead of $title = $titleraw->title;

Upvotes: 1

Bruno Photonautes
Bruno Photonautes

Reputation: 31

Simply

$mypage=file_get_html('http://myurl.com');
$title=$mypage->find('title',0);
echo $title->plaintext;

Upvotes: 2

Janak Prajapati
Janak Prajapati

Reputation: 886

try this

$html = new simple_html_dom()
$data = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('title') as $element)
       echo $element->plaitext . '<br>';

Upvotes: 0

Shailendra Sharma
Shailendra Sharma

Reputation: 6992

Try

include_once 'simple_html_dom.php';
$oHtml = str_get_html($url);
$Title = array_shift($oHtml->find('title'))->innertext;
$Description = array_shift($oHtml->find("meta[name='description']"))->content;
$keywords = array_shift($oHtml->find("meta[name='keywords']"))->content;
echo $title;
echo $Description;
echo $keywords;

Upvotes: 0

chuck911
chuck911

Reputation: 785

$html = new simple_html_dom();
$html->load_file('http://www.google.com'); 
$titleraw = $html->find('title',0);
$title = $titleraw->innertext;

$html->load_file() Loads contents from a from a file or a URL.

$html->find('title') will return an array

and $titleraw->innertext returns content of title element

Upvotes: 3

anubhava
anubhava

Reputation: 784958

Using DOM and xpath you can do:

function getTitle($url) {
   libxml_use_internal_errors(true);
   $doc = new DOMDocument();
   $doc->loadHTMLFile($url);
   $xpath = new DOMXPath($doc);
   $nlist = $xpath->query("//head/title");
   return $nlist->item(0)->nodeValue;
}
echo "Title: " . getTitle("http://www.google.com") . "\n";

Upvotes: 0

powtac
powtac

Reputation: 41040

if(
  preg_match(
    '~<title>(.*)</title>~si',
    file_get_contents('http://www.google.com'),
    $result
  );
  var_dump($result[1]);
}else{ /* no result */ }

Else

$titleraw = $html->xpath('//title');

Upvotes: 1

Marc B
Marc B

Reputation: 360572

->load() expects a string containing HTML, not a URL.

Try:

$html = file_get_html('http://google.com');

instead.

Beyond that, note that google's ToS forbid screen scrapers, so hopefully you're just using that url as a fill-in example rather than whatever you're really trying to scrape.

Upvotes: 2

Related Questions