| 301 | | // Elements that should not be surrounded by p tags |
| 302 | | $no_p = '(?:p|div|h[1-6r]|ul|ol|li|blockquote|d[dlt]|pre|t[dhr]|t(?:able|body|foot|head)|c(?:aption|olgroup)|form|s(?:elect|tyle)|a(?:ddress|rea)|ma(?:p|th))'; |
| 303 | | |
| 304 | | // Put at least two linebreaks before and after $no_p elements |
| 305 | | $str = preg_replace('~^<'.$no_p.'[^>]*+>~im', "\n$0", $str); |
| 306 | | $str = preg_replace('~</'.$no_p.'\s*+>$~im', "$0\n", $str); |
| | 301 | // The following regexes only need to be executed if the string contains html |
| | 302 | if (($html_found = strpos($str, '<')) !== FALSE) |
| | 303 | { |
| | 304 | // Elements that should not be surrounded by p tags |
| | 305 | $no_p = '(?:p|div|h[1-6r]|ul|ol|li|blockquote|d[dlt]|pre|t[dhr]|t(?:able|body|foot|head)|c(?:aption|olgroup)|form|s(?:elect|tyle)|a(?:ddress|rea)|ma(?:p|th))'; |
| | 306 | |
| | 307 | // Put at least two linebreaks before and after $no_p elements |
| | 308 | $str = preg_replace('~^<'.$no_p.'[^>]*+>~im', "\n$0", $str); |
| | 309 | $str = preg_replace('~</'.$no_p.'\s*+>$~im', "$0\n", $str); |
| | 310 | } |
| 312 | | // Remove p tags around $no_p elements |
| 313 | | $str = preg_replace('~<p>(?=</?'.$no_p.'[^>]*+>)~i', '', $str); |
| 314 | | $str = preg_replace('~(</?'.$no_p.'[^>]*+>)</p>~i', '$1', $str); |
| | 316 | // The following regexes only need to be executed if the string contains html |
| | 317 | if ($html_found !== FALSE) |
| | 318 | { |
| | 319 | // Remove p tags around $no_p elements |
| | 320 | $str = preg_replace('~<p>(?=</?'.$no_p.'[^>]*+>)~i', '', $str); |
| | 321 | $str = preg_replace('~(</?'.$no_p.'[^>]*+>)</p>~i', '$1', $str); |
| | 322 | } |