0

I would like to append some text to a dom element as a child node.

The problem is that in the text I can have tags as <i>, <bold> etc.

Actually, with this method:

private function appendChildNode($dom_output, $cit_node, $nodeName, $nodeText)
{
    if ($nodeText != null && $nodeText != "" ) {
        $node = $dom_output->createElement($nodeName);
        $node->appendChild($dom_output->createTextNode($nodeText));
        $cit_node->appendChild($node);
        return $node;
    }
}

When I have tags in the $nodeText, then the <i> will be converted to &lt;i&gt; and so on.

How can I append this text by keeping the tags as they are?

Thank you.

1 Answer 1

0

I managed to find the solution.

Thanks to this post: DOMDocument append already fixed html from string

I did:

private function appendChildNode($dom_output, $cit_node, $nodeName, $nodeText)
{
    if ($nodeText != null && $nodeText != "" ) {        
        $node = $dom_output->createElement($nodeName);
        $fragment = $dom_output->createDocumentFragment();
        $fragment->appendXML( $nodeText);
        $node->appendChild($fragment);
        $cit_node->appendChild($node);
        return $node;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.