How do I extract title of a website? [duplicate]
Asked Answered
C

3

0

Possible Duplicate:
Best methods to parse HTML

How do I extract title of a website using PHP?

Coccid answered 3/5, 2011 at 11:57 Comment(1)
Are you referring to the HTML title e.g. <head><title>VALUE</title></head> ? If so you should be setting that on the server side when the response goes back.Sexdecillion
S
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];
Sperry answered 3/5, 2011 at 12:2 Comment(1)
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.Idolater
D
0

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

Duologue answered 3/5, 2011 at 11:58 Comment(1)
doubtful if OP refers to domain name though.Idolater
D
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).

Dentate answered 3/5, 2011 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.