3v4l.org

run code in 300+ PHP versions simultaneously
<?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"; }

preferences:
34.23 ms | 402 KiB | 5 Q