- preg_replace: documentation ( source)
<?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";
}