0

How could I replace via preg_replace a all the tables on a string? That means also the tables nested on other tables. Currently I'm using this code:

$pattern = '/(<table[^>]*>)(.*?)(<\/table>)/is';

but is not "cleaning" or replacing all the tables.

Anybody has an idea?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Suggestion: use DOM for parsing HTML. Check http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags :) – Maxime Pacary Mar 22 '11 at 10:47
  • Possible duplicate of [Using regular expressions to parse HTML: why not?](https://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not) – Brian Tompsett - 汤莱恩 Nov 05 '17 at 18:10

1 Answers1

0

Your regex will match opening of table, and closing of nested table as far as I can see. Replacing nested tables is a little complicated - what are you going to replace it with exactly? How do you plan to handle nesting?

If you are replacing the TABLE tags - why not treat them seperately, so replace all the opening tags with whatever, then all the closing tags with closing whatever. This will maintain the nesting.

Billy Moon
  • 57,113
  • 24
  • 136
  • 237