PHP DOMElement, reemplazando el texto de un nodo

I have a HTML node like so:

<b>Bold text</b>

A variable $el contains a DOMElement reference to the text of that HTML node ("Bold text"), got from the XPath expression //b/text()

I want to change the element to

<b><span>Bold Text</span></b>

Así que intenté:

$span = $doc->createElement('span', "Bold Text");
$el->parentNode->replaceChild($span,, $el)

which fails because parentNode is null.

So, as a test, I tried: $el->insertBefore($span, $el);

which throws no errors but produces no change in the output.

¿Alguna idea?

preguntado el 08 de enero de 11 a las 21:01

1 Respuestas

DOMXPath->query() using //b/text() should return a DOMNodeList. Get an item using the item() method. It should be a DOMText, which is a DOMNode and parentNode shouldn't be null.

Respondido el 09 de enero de 11 a las 00:01

Thanks, I filed a bug with at php.net, and they appended the SimpleXML documentation to indicate that /text() XPath queries don't return objects in the same format as other queries - bcoughlan

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