简体   繁体   中英

Insert HTML into existing HTML file using PHP

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM