Milos Cuculovic
Milos Cuculovic

Reputation: 20223

How to insert TextNode after a specific node.

I have a paragraph:

<p>
   This is a test <xref>1</xref> in the XML <xref>2</xref> bla bla bla....
</p>

I would like to insert ] after the xref node.

For that, I am using the following code:

$refs = $paragraph->getElementsByTagName("xref");

foreach ($refs as $key=>$ref) {
   $squareBracket = $dom_input->createTextNode("]");
   $paragraph->appendChild($squareBracket);
}

But this is adding the ] at the end of the paragraph.

Is there any function as insertBefore but to insert after?

Upvotes: 0

Views: 128

Answers (1)

Milos Cuculovic
Milos Cuculovic

Reputation: 20223

I found how to do it:

$squareBracket = $dom_input->createTextNode("]");
$paragraph->insertBefore($squareBracket, $ref->nextSibling);

We have to use nextSibling on the selected node.

Upvotes: 2

Related Questions