note 109577 deleted from function.nl2br by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 11:10:07 +0000 |
| Subject: | note 109577 deleted from function.nl2br by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195177@lists.php.net to get a copy of this message | ||
Note Submitter: ben at indietorrent dot org
----
If you're looking to do the reverse, that is, "break to newline", here's one
approach, which uses regular expressions and enables breaks to be replaced with any character (not
just a newline). The expression includes any amount of white-space between the characters in the
HTML break tags(s) and disregards case (that is, <br/> and <BR > will both be replaced),
and takes into account the optional "short" closing tag.
<?php
function replaceHtmlBreaks($str, $replace, $multiIstance = FALSE)
{
//The multi-instance version, which replaces any number
//of consecutive tags, including any white-space, will replace
//
//Some string <BR /><br> Another string <br /><Br />
//
//for example, with
//
//Some string.Another string.
//
//The single-instance version will replace the same string with
//
//Some string .. Another string ..
$base = '<[bB][rR][\s]*[/]*[\s]*>';
$pattern = '|' . $base . '|';
if ($multiIstance === TRUE) {
//The pipe (|) delimiter can be changed, if necessary.
$pattern = '|([\s]*' . $base . '[\s]*)+|';
}
return preg_replace($pattern, $replace, $str);
}
?>