magic! $str = '
'.trim($str).'
", $str); // The following regexes only need to be executed if the string contains html if ($html_found !== FALSE) { // Remove p tags around $no_p elements $str = preg_replace('~
(?=?'.$no_p.'[^>]*+>)~i', '', $str); $str = preg_replace('~(?'.$no_p.'[^>]*+>)