Search code examples
phpdomdocumentgetelementsbytagname

getElementsByTagName title is coming back with DOMNodeList Object


Our script uses dom to parse all the a tags from a document then loops through child nodes and extracts information which works fine here's how the code starts

@$dom->loadHTML($str);
$documentLinks = $dom->getElementsByTagName("a");

Part of the loop

$this->count]['href']     = strip_tags($documentLink->getAttribute('href'));

I now need to get the title tag from each page were lopping through so I thoguht I could do

$documentTitle = $dom->getElementsByTagName("title");
$documentLinks = $dom->getElementsByTagName("a");

Then add this to the loop/array to get the document title but it comes back with "[title] => DOMNodeList Object()" How can I include the title tag in the loop which is going through a tags/child nodes?

$this->count]['title']  = $documentTitle;

Solution

  • getElementsByTagName returns a DOMNodeList object. You want the text content of the first (should only be one page title) item in the list.

    Try this:

    $documentTitle = $dom->getElementsByTagName('title')->item(0)->textContent;