3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $strings[] = 'foo<p> <br> </p>'; $strings[] = 'foo<p> <br> </p>'; $strings[] = 'foo<p><br><br> </p>'; $strings[] = 'foo<p> <br> </p>'; foreach($strings as $string){ // \s* matches any number of whitespace characters (" ", \t, \n, etc) // (?:...)+ matches one or more (without capturing the group) // $ forces match to only be made at the end of the string $string = preg_replace("/(?:<p>\s*(?:<br>\s*)+<\/p>\s*)+$/", "", $string); echo $string."\n---\n"; }
Output for 4.3.0 - 7.1.0
foo --- foo --- foo --- foo ---