Search code examples
phpgetattribute

PHP Attribute for innerhtml


Is there any way to select the innerHTML via getAttribute in PHP. I tried this already but it isn't working:

$fetchresult[] = array($link->getAttribute("innerhtml"), $link->nodeValue);

when i var_dump $link this is the output:

object(DOMElement)#7 (18) { ["tagName"]=> string(3) "div" 
["schemaTypeInfo"]=> NULL ["nodeName"]=> string(3) "div" ["nodeValue"]=> 
string(24) "TEXT" ["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"]=> NULL 
["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" 
["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> 
NULL ["prefix"]=> string(0) "" ["localName"]=> string(3) "div" ["baseURI"]=> NULL 
["textContent"]=> string(24) "TEXT" }

Thanks in advance


Solution

  • innerHTML is not an attribute, it's a non-standard DOM extension that serializes (when used as a getter) the child nodes of the element to a string of HTML.

    Going purely from reading the PHP manual, the equivalent would appear to be:

    1. Create an empty string in a variable ($html = "");
    2. Loop over childNodes
    3. Append the HTML of that node to the variable ($html .= $Node->ownerDocument->saveHTML( $Node );)