0

I am trying to loop through nodeValues of objects outputed by DOMDocument and get the text output but I am not sure how to go about doing it. My code and output are below.

From the output below I need to get only the NodeValue text output, e.g. "Bar and Hotel".

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

                $xpath = new DomXpath($dom);
                $div = $xpath->query('//*[@class="type-cat"]'); 
                foreach ($div as $a) {

                    var_dump($a );      
                }

object(DOMElement)#22 (18) { 
    ["tagName"]=> string(2) "h5"
    ["schemaTypeInfo"]=> NULL
    ["nodeName"]=> string(2) "h5"
    ["nodeValue"]=> string(41) " Bar and Hotel" 
    ["nodeType"]=> int(1)
    ["parentNode"]=> string(22) "(object value omitted)" 
    ["childNodes"]=> string(22) "(object value omitted)" 
    ["firstChild"]=> string(22) "(object value omitted)"
    ["lastChild"]=> string(22) "(object value omitted)"
    ["previousSibling"]=> string(22) "(object value omitted)"
    ["nextSibling"]=> string(22) "(object value omitted)" 
    ["attributes"]=> string(22) "(object value omitted)" 
    ["ownerDocument"]=> string(22) "(object value omitted)" 
    ["namespaceURI"]=> NULL 
    ["prefix"]=> string(0) "" 
    ["localName"]=> string(2) "h5" 
    ["baseURI"]=> NULL
    ["textContent"]=> string(41) " Bar and Hotel" 
}
Leif Neland
  • 1,416
  • 1
  • 17
  • 40
condo1234
  • 3,285
  • 6
  • 25
  • 34

1 Answers1

2

As you can see from the dump of the node object, the nodeValue is what you are looking for. You access it with

$a->nodeValue;

Depending on the nodeType property this will return the text value.

You find this and all other public accessible properties and methods here:

http://www.php.net/manual/de/class.domnode.php

Nicolas Zimmer
  • 424
  • 4
  • 10