¿Cómo generar texto de elementById en DOM?

It should be very simple. I am loading in php via DOMDocument();

    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $el = $doc->getElementById('somethingId');

Digamos que tengo

<html><head></head><body><div id="somethingId">my
<span style="background:red">something else</span> 
information</div></body></html>

Q1. How to echo whats inside that element ("my information") from $el?

Q2. How to echo whats inside and including span data (like innerHTML in javascript)?

Respuesta a la P2:

$children = $el->childNodes;

foreach ($children as $child) { 

    $innerHTML .= $child->ownerDocument->saveXML( $child ); 

}

echo $innerHTML

preguntado el 09 de marzo de 12 a las 13:03

2 Respuestas

You should do

echo ($el->nodeValue);

respondido 09 mar '12, 13:03

are brackets necessary? also i have added another question. Can you help. Thanks - TroodoN-Mike

@TroodoN-Mike: No, the brackets are not necessary. echo is not a function - it's a language construct. - Marc B

if (!is_null($el)) {
        $content = $el->nodeValue;

        if (empty($content)) {
            $content = $el->textContent;
        }

        echo $content;
    }

respondido 09 mar '12, 13:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.