0

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!

  • http://stackoverflow.com/questions/4350141/find-an-element-by-id-and-replace-its-contents-with-php – iiro Sep 08 '13 at 19:24

2 Answers2

1

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.

zoltar
  • 706
  • 2
  • 7
  • 20
1

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

Community
  • 1
  • 1
Hansjörg Hofer
  • 409
  • 1
  • 5
  • 18