-3

I would like to write a regular expression rule to replace everything in between and included the table tag.

For example:

<table class="table1"><tr><td></td></tr></table>

Everything above to be replaced in a string, including the table tag

Using preg_replace

Jenz
  • 8,009
  • 6
  • 38
  • 69
davidlee
  • 3,294
  • 13
  • 47
  • 78

1 Answers1

1

See these two examples.

1.) Match innermost tables:

$pattern = '~<table(?>(?!</?table).)*</table>~is';

test at regex101


2.) Match table and if nested tables inside: Using a recursive pattern

$pattern = '~<table((?>(?!</?table).)*|(?R))+</table>~is';

test at regex101


So just use preg_replace to replace:

$str = preg_replace($pattern, "...", $str);

test at eval.in


For further explanation also see: SO Regex FAQ

Community
  • 1
  • 1
Jonny 5
  • 11,051
  • 2
  • 20
  • 42