FishSword Posted February 19, 2012 Share Posted February 19, 2012 Hello there, Using PHP, what's the best way of replacing the contents between different HTML 5 element tags on the fly? Some of the elements I'm looking to change can be found below. I have looked into the possibility of using Regular Expressions, but have found lots of material on the internet that try to prevent you from using such a method for parsing HTML due to speed and other HTML code related issues. Any help, example code, and ideas on this is greatly appreciated. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Some Title</title> </head> <body> <div>Replace contents of div tag.</div> <p class="someClass">Replace contents of p tag.</p> <table> <tr> <th>Static Table Heading 1</th> <th>Static Table Heading 2</th> <th>Static Table Heading 3</th> </tr> <tr> <td>Replace Table Data 1 Contents</td> <td>Replace Table Data 2 Contents</td> <td>Replace Table Data 3 Contents</td> </tr> <tr> <td>Replace Table Data 4 Contents</td> <td>Replace Table Data 5 Contents</td> <td>Replace Table Data 6 Contents</td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/257304-replace-html-5-tag-contents/ Share on other sites More sharing options...
kicken Posted February 19, 2012 Share Posted February 19, 2012 Load the document using the DOMDocument class, then you can manipulate it's contents using the DOM api by adding/removing/changing nodes. When your done you can save it out again. Quote Link to comment https://forums.phpfreaks.com/topic/257304-replace-html-5-tag-contents/#findComment-1318931 Share on other sites More sharing options...
FishSword Posted February 19, 2012 Author Share Posted February 19, 2012 Thanks for this. Would it be possible to post an example on how I would replace the contents of the tags shown in my first post? Quote Link to comment https://forums.phpfreaks.com/topic/257304-replace-html-5-tag-contents/#findComment-1318948 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.