得到一个页面的标题

时间:2013-08-12 20:00:53

标签: php html-parsing

我使用以下

获取页面标题
$urlContents = file_get_contents("$url");
preg_match("/<title>(.*)<\/title>/i", $urlContents, $matches);

我遇到的问题是标题为3行的网站之一

<head id="head"><title>
    title goes here
</title><meta name="description" content="meta description" /> 

这是我正在写的一个基本的onpage seo工具,有没有更好的方法可以获得页面的标题?

谢谢

3 个答案:

答案 0 :(得分:1)

如何使用普通的js

var title = document.title

或者jquery

var title = $("head title")[0];

答案 1 :(得分:0)

我在这里找到了答案

Get title of website via link

$urlContents = file_get_contents("http://example.com/");

$dom = new DOMDocument();
@$dom->loadHTML($urlContents);

$title = $dom->getElementsByTagName('title');

print($title->item(0)->nodeValue . "\n"); // "Example Web Page"

谢谢

答案 2 :(得分:-2)

我认为标题有字符限制。因此标题不应为三行。我已经签入Yoast插件。它也说一样。那么获得三行seo标题的目的是什么。