0

Possible Duplicate:
Best methods to parse HTML

How do I extract title of a website using PHP?

Community
  • 1
  • 1
alwyn
  • 75
  • 1
  • 2
  • 6
  • Are you referring to the HTML title e.g. VALUE ? If so you should be setting that on the server side when the response goes back. – planetjones May 03 '11 at 12:00

3 Answers3

5

To manupulate with HTML you can use Document Object Model (this is the recomended way).

If you are looking for a dirty fast solution, it's rather simple regexp:

$html = file_get_contents('http://example.com');
$matches = preg_match("/<title>([^<]*)<\/title>/im");
echo $matches[1];
Silver Light
  • 44,202
  • 36
  • 123
  • 164
  • could at least have shown the undirty version with DOM then (which is just one LOC more). Now the OP will copy and paste the Regex solution. – Gordon May 03 '11 at 12:19
0

You can get the domain name through $_SERVER['HTTP_HOST'].

Zach Rattner
  • 20,745
  • 9
  • 59
  • 82
0

Grab the document using curl, run it through a parser and then use whatever API that parser provides to get the title element (//title will do if there is XPath support).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335