Argh. This should be something very simple, but I can’t figure it out. I have a simple PHP script that’s supposed to add a banner right after the BODY tag of an HTML file:
<?php
$file = new DOMDocument();
$file->loadHTMLfile('myfile.html');
$body = $file->getElementsByTagName('body')->item(0);
$banner = $file->createElement('div', 'Back to the <a href="/">home page.</a>');
$body->insertBefore($banner, $body->firstChild);
echo $file->saveHTML();
?>
I just want the banner to provide a link back to the root home page. The relevant line is:
$banner = $file->createElement('div', 'Back to the <a href="/">home page.</a>');
Problem is, that entire line is being treated as a string and displayed as such. I guess PHP is treating $banner as a DOM object (since I defined it as such), but what I really want it to do is treat it as HTML code. In JavaScript, this would be the “innerHTML” property… how would I do this in PHP?
Thank you!
ETA: To be clear, I want it to show up like this:
Back to the home page.
And NOT like:
Back to the <a href="/">home page</a>.
I’ve never actually used the DOMDocument in PHP so I’m guessing here. Maybe there is a way to tell it not to escape the < and > characters you enter but …
I think you’re forgetting that the link itself is just another element. Wouldn’t it be more logical and consistent to add the ‘a’ element as a real element, not as html? If you really need the div then you’d add the div like you have already done and then add the a to the newly created div by using createElement again. Or if you really don’t need the div then just add the a instead of the div.
I thought of that, but the thing is, I want to do something like a <div><p>blah blah<a>blah blah</a></p><p>test</p></div> and having to add all that individually is… retarded. There must be a better way, no?