# PHP equivalent to innerHTML?

**URL:** <https://boards.straightdope.com/t/php-equivalent-to-innerhtml/555197>\
**Category:** Factual Questions\
**Created:** [September 27, 2010, 8:10am UTC](https://boards.straightdope.com/t/php-equivalent-to-innerhtml/555197 "2010-09-27T08:10:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [September 27, 2010, 8:10am UTC](https://boards.straightdope.com/t/php-equivalent-to-innerhtml/555197/1 "2010-09-27T08:10:49Z")

</div>

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

<?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:

```php

$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\>.

---

<div class="post-metadata">

**Author:** ![tetranz](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@tetranz](https://boards.straightdope.com/u/tetranz)\
**Post date:** [September 27, 2010, 11:19am UTC](https://boards.straightdope.com/t/php-equivalent-to-innerhtml/555197/2 "2010-09-27T11:19:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [September 27, 2010, 11:23am UTC](https://boards.straightdope.com/t/php-equivalent-to-innerhtml/555197/3 "2010-09-27T11:23:29Z")

</div>

> [@tetranz](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![Markxxx](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@Markxxx](https://boards.straightdope.com/u/Markxxx)\
**Post date:** [September 27, 2010, 1:00pm UTC](https://boards.straightdope.com/t/php-equivalent-to-innerhtml/555197/4 "2010-09-27T13:00:54Z")

</div>

You may want to ask this at [Dynamic Drive (dot) Com](http://www.dynamicdrive.com/forums/). You go to the forums and look for the php section. They’re very good and very helpful too.

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [September 27, 2010, 4:25pm UTC](https://boards.straightdope.com/t/php-equivalent-to-innerhtml/555197/5 "2010-09-27T16:25:04Z")

</div>

Figured it out. The answer is to use **createCDATASection**.
