• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

Insert HTML into PHP DOM object

August 30, 2012 by Webhead

Problem

I recently had an html string that needed to be added to a DOM object.   At first I had something like this:

$dom = new DOMDocument;
$field_div = $dom->createElement(“div”, $field);
$dom->appendChild($field_div);

Where $field was an html string.  This produced a div with the html printed out as text.

Solution

Stack overflow is just the best website ever created for developers.  The solution is to use DOM->createDocumentFragment.  So the new code looked like:

$field_html = $dom->createDocumentFragment(); // create fragment
$field_html->appendXML($field);
$field_div = $dom->createElement(“div”);
$field_div->appendChild($field_html);

source: http://stackoverflow.com/questions/2255158/how-do-i-insert-html-into-a-php-dom-object
keywords: php domnode create node from html string

Filed Under: Coding Tagged With: php

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization mac os x ms sql mysql open source optimize php php 5.3 responsive rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools