使用PHP将HTML插入现有HTML文件

So I wanna insert an html element that I have as a string into an existing HTML file on the server. So I already have my string, I get a handle on the file using $handle = fopen("file.php", "rw"); and I want to add the string $content that contains the HTML in the existing file after the div element that has the id "inner_container".

What is the best way to do this ?

Let me know if you need code. Basically what I wanna do is insert HTML after a certain element that Id find using its id.

Thanks!

You should try this one here :

How can I use XPath and DOM to replace a node/element in php?

Answer is all about your problem :)

Basically, you need to parse the HTML first (and you don't want to use REGEX for this as I learned in the past -- it may work in many cases, but it's not nearly as clean and friendly as using a DOM parser).

I suggest you use simple_html_dom http://simplehtmldom.sourceforge.net/ to parse the HTML of the file first, then add your code to it and save. You can do this with built in functionality of simple_html_dom.