开发者

Grabbing title of a website using DOM [duplicate]

This question already has answers here: Closed 11 years ago.开发者_如何学C

Possible Duplicates:

Get title of website via link

How do I extract title of a website?

How can a website's title be grabbed using PHP DOM? (Which is the best way to grab it using PHP?)


You can use the getElementByTagName() since there is technically only a single title attribute in your html so you can just grab the first one you come across in DOM.

$title = '';
$dom = new DOMDocument();

if($dom->loadHTMLFile($urlpage)) {
    $list = $dom->getElementsByTagName("title");
    if ($list->length > 0) {
        $title = $list->item(0)->textContent;
    }
}


Suppresses any parsing errors from incorrect HTML or missing elements:

<?

$doc = new DOMDocument();
@$doc->loadHTML(@file_get_contents("http://www.washingtonpost.com"));

// find the title
$titlelist = $doc->getElementsByTagName("title");
if($titlelist->length > 0){
  echo $titlelist->item(0)->nodeValue;
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜