开发者

PHP DOMElement, replacing text of a node

开发者 https://www.devze.com 2023-02-03 00:37 出处:网络
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 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><开发者_如何学Python;span>Bold Text</span></b>

So I tried:

$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.

Any thoughts?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消