得到一个页面的标题

问题描述 投票:0回答:3

我正在使用以下内容获取页面标题

$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工具,有没有更好的方法我可以获得页面的标题?

谢谢

php html-parsing
3个回答
1
投票

如何使用普通的js

var title = document.title

或者jquery

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

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
投票

我认为标题有字符限制。所以标题不应该分为三行。我已经检查了Yoast插件。它也说同样的话。然后获得三行seo标题的目的是什么。

© www.soinside.com 2019 - 2024. All rights reserved.