Capturing website header using DOM

Possible duplicates:
Get the site name from the link
How to extract website name?

How can I grab a site name using PHP DOM? (What's the best way to capture it using PHP?)

+6
source share
2 answers

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

$title = ''; $dom = new DOMDocument(); if($dom->loadHTMLFile($urlpage)) { $list = $dom->getElementsByTagName("title"); if ($list->length > 0) { $title = $list->item(0)->textContent; } } 
+14
source

Suppresses any parsing errors from invalid 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; } 
+4
source

Source: https://habr.com/ru/post/887196/


All Articles