How to Insert HTML to PHP Domnode

How to insert HTML to PHP DOMNode?

It works with another DOMDocument for parsing the HTML code. But you need to import the nodes into the main document before you can use them in it:

$newDiv = $dom->createElement('div');
$tmpDoc = new DOMDocument();
$tmpDoc->loadHTML($str);
foreach ($tmpDoc->getElementsByTagName('body')->item(0)->childNodes as $node) {
$node = $dom->importNode($node, true);
$newDiv->appendChild($node);
}

And as a handy function:

function appendHTML(DOMNode $parent, $source) {
$tmpDoc = new DOMDocument();
$tmpDoc->loadHTML($source);
foreach ($tmpDoc->getElementsByTagName('body')->item(0)->childNodes as $node) {
$node = $parent->ownerDocument->importNode($node, true);
$parent->appendChild($node);
}
}

Then you can simply do this:

$elem = $dom->createElement('div');
appendHTML($elem, '<h1>Hello world</h1>');

How do I insert HTML into a PHP DOM object?

I spent a lot of time working on Anthony Forloney's answer, But I cannot seem to get the html to append to the body without it erroring.

@Mark B: I have tried doing that, but as I said in the comments, it errored on my html.

I forgot to add the below, my solution:

I decided to make my html object much simpler and to allow me to do this by not using DOM and just use strings.

How to insert HTML into a text node with DOMNode in PHP

I got around this a little clumsily, but it works for now. If there are better solutions, I'll happily modify my answer!

Essentially, this looks for an opening tag "<" character within the substitution text. If it finds one, it invokes an HTML substitution method which I modified from this question, answer and comments.

It has a limitation in that it can't substitute HTML mid-node. For example, the following will not work:

<p>Here is a bit of {html_code}</p>

But that can be made to work like this:

<p>Here is a bit of <span>{html_code}</span></p>

Here is the modified code:

private function substituteFields (DOMNode $node, $fields)
{
$x = new DOMXPath ($node->ownerDocument);
foreach ($fields as $field => $value)
{
$query = $x->query (".//text()[contains(., '{" . $field . "}')]", $node);

foreach ($query as $subnode)
{
$replace = str_replace ("{" . $field . "}", $value, $subnode->nodeValue);

if (substr ($replace, 0, 1) != "<")
{
$subnode->nodeValue = $replace;
}
else
{
$this->appendHTML ($subnode, $replace);
}
}
}
}

private function appendHTML (DOMNode $parent, $source)
{
$tmpDoc = new DOMDocument();
$tmpDoc->loadHTML ($source);
foreach ($tmpDoc->getElementsByTagName ('body')->item (0)->childNodes as $node)
{
$importedNode = $parent->ownerDocument->importNode ($node, true);
$parent->parentNode->replaceChild ($importedNode, $parent);
}
}

How to convert HTML string to DOMNode to use DOMNode::insertBefore in PHP?

The solution si to use createdocumentfragment like this :

Template object´s method

public function write(&$htmlTmpl)
{

$html_advanced_options = '<a href="#">My link</a>';
$html_advanced_options .= 'other html';
...

$tmpl_advanced_options = $htmlTmpl->createDocumentFragment();
$tmpl_advanced_options->appendXML( $html_advanced_options );

$nodeLine = $htmlTmpl->getElementsByTagName("hr")->item(0);
$parentNode = $htmlTmpl->getElementsByTagName("div")->item(0);
$parentNode->insertBefore( $section_advanced_options, $nodeLine );
$parentNode->insertBefore( $tmpl_advanced_options, $section_advanced_options );

}

Php - Dom, Insert HTML_CODE inside nodeValue

In order to add new parts of the XML, you'll need to create DOM nodes out of them somehow instead of using straight text.

You might try using DOMDocument Fragment: http://www.php.net/manual/en/domdocumentfragment.appendxml.php

Or, as it suggests on that page, create an additional DOMDocument object and loop through the nodes to add them to your original DOMDocument.

PHP DomNode- insertBefore()

The code doesn't make any sense. DOMNode does not have a constructor. It is not supposed to be created at all. You are supposed to create specific node types through DOMDocument to have them associated with the Document.

Assuming you want to prepend all the H2 element with an anchor, this is how to do it:

libxml_use_internal_errors(true);
$DOM = new DOMDocument();
$DOM->loadHTML($htmlString);
$DOM->preserveWhiteSpace = false;

foreach ($DOM->getElementsByTagName('h2') as $h2) {
$a = $DOM->createElement('a');
$a->setAttribute('name', $h2->nodeValue);
$h2->parentNode->insertBefore($a, $h2);
}
$DOM->formatOutput = true;
echo $DOM->saveHTML();

Demo http://codepad.org/N0dPcLwT

To wrap the H2 elements into the A element, simply do the same and add

$a->appendChild($h2);

Demo http://codepad.org/w7Hi0Bmz

Append HTML Strings to DOMDocument at start and end of DOMXPath

Here's one way to do it. It makes use of DOMDocumentFragment. Admittedly, it's all a bit convoluted.

Furthermore, one problem with DOMDocumentFragment is that, besides appendXML(), it does not have a counterpart method appendHTML(), which means you can only append HTML to it if it validates as XML.

$html = '<div class="importantnoticediv">
<p class="important-notice-title important-notice-title-1">
NOTICE:
</p>
<p class="important-notice-title important-notice-title-2">
PROFESSIONAL INSTALLATION IS RECOMMENDED!
</p>
</div>';

$dom = new DOMDocument;
// format our output to make it a tad bit easier on the eyes (doesn't help all that much, though)
$dom->formatOutput = true;
// this implicitly adds <html> and <body> tags
$dom->loadHTML( $html );

$xpath = new DOMXpath( $dom );

// notice I appended /p to your original query, so that we get the list of <p> elements inside the "importantnoticediv"
$nodes = $xpath->query('//div[contains(@class, "importantnoticediv")]/p');
// the first <p>
$p1 = $nodes->item( 0 );
// the second <p>
$p2 = $nodes->item( 1 );

// we create a DOMDocumentFragment that is associated with our DOMDocument
$frag = $dom->createDocumentFragment();

// I append your first HTML fragment (must be valid XML)
$frag->appendXML( '<div class="note-icon note-icon-important">
<span><i class="fa fa-exclamation" aria-hidden="true"></i>
</span>
</div>' );
// ... and insert it before our first <p> (parentNode is the "importantnoticediv")
$p1->parentNode->insertBefore( $frag, $p1 );

// frag is now empty again, because we just inserted it(s contents) into the DOMDocument

// we create our second fragment
$frag->appendXML( '<div class="note-text note-text-important"></div>' );

// this time we append it to "importantnoticediv" (i.e. as last child)
// and get the reference to this newly inserted <div>
$insertedNode = $p1->parentNode->appendChild( $frag );

// now we can append (i.e. move) the <p> elements to the newly inserted <div>
$insertedNode->appendChild( $p1 );
$insertedNode->appendChild( $p2 );

// the HTML should now be as desired
echo $dom->saveHTML();

How to add text node in new created element with PHP DOMDocument

You can set the content using $textContent property of DOMElement.

$newElement = $finalDom->createElement("script");
$newElement->setAttribute("src", "https://stackoverflow.com/");

$newElement->textContent = 'alert("ok");';

How to append source html to a DOMElement in PHP?

You are looking for

- DOMDocumentFragment::appendXML — Append raw XML data

Example from Manual:

$doc = new DOMDocument();
$doc->loadXML("<root/>");
$f = $doc->createDocumentFragment();
$f->appendXML("<foo>text</foo><bar>text2</bar>");
$doc->documentElement->appendChild($f);
echo $doc->saveXML();


Related Topics



Leave a reply



Submit