I am using PHP's DOM object to create HTML pages for my website. This works great for my head, however since I will be entering a lot of HTML into the body (not via DOM), I would think I would need to use DOM->createElement($bodyHTML)
to add my HTML from my site to the DOM object.
However DOM->createElement
seems to parse all HTML entities so my end result ended up displaying the HTML on the page and not the actual renders HTML.
I am currently using a hack to get this to work,
$body = $this->DOM
->createComment('DOM Glitch--><body>'.$bodyHTML."</body><!--Woot");
Which puts all my site code in a comment, which I bypass athe comment and manually add the <body>
tags.
Currently this method works, but I believe there should be a more proper way of doing this. Ideally something like DOM->createElement()
that will not parse any of the string.
I also tried using DOM->createDocumentFragment()
However it does not like some of the string so it would error and not work (Along with take up extra CPU power to re-parse the body's HTML).
So, my question is, is there a better way of doing this other than using DOM->createComment()
?
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.
You use the DOMDocumentFragment objec to insert arbitrary HTML chunks into another document.
I FOUND THE SOLUTION but it's not a pure php solution, but works very well. A little hack for everybody who lost countless hours, like me, to fix this
loadHTML
works just fine.which outputs Hey there mrlanrat! in red.
or
Which outputs:
here is the body, a nice body I might add and inside of your HTML source code, its wrapped inside
body
tags.