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

2019-03-04 18:42发布

Possible Duplicate:
Best methods to parse HTML

How do I extract title of a website using PHP?

3条回答
贪生不怕死
2楼-- · 2019-03-04 19:22

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

查看更多
ら.Afraid
3楼-- · 2019-03-04 19:23

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).

查看更多
闹够了就滚
4楼-- · 2019-03-04 19:35

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];
查看更多
登录 后发表回答