note 39615 added to function.nl2br
| From: | greywyvern-greywyvern-com at rn2 dot php dot net | Date: | Wed, 04 Feb 2004 19:05:01 +0000 |
| Subject: | note 39615 added to function.nl2br | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64646@lists.php.net to get a copy of this message | ||
In posting some script formatted data to the screen, I noticed that if you nl2br a string containing
<pre> tags, the lines within would be double-spaced; once for the \n which is preserved by
<pre> and once for the <br />.
So I needed a way to preform a nl2br on an entire message, but exclude or revert <pre> to just
normal \n line-endings. This way, the text within <pre> tags would remain single spaced.
string nl2br_pre(string input [, int wrap])
Here's the function I made to do that. It also wraps <pre> lines to $wrap characters
wide, which you can adjust.
<?php
function nl2br_pre($string, $wrap = 40) {
$string = nl2br($string);
preg_match_all("/<pre[^>]*?>(.|\n)*?<\/pre>/", $string, $pre1);
for ($x = 0; $x < count($pre1[0]); $x++) {
$pre2[$x] = preg_replace("/<br[^>]*?>/", "", $pre1[0][$x]);
$pre2[$x] = preg_replace("/([^\n]{".$wrap."})(?!<\/pre>)/",
"$1\n", $pre2[$x]);
$pre1[0][$x] = "/".preg_quote($pre1[0][$x], "/")."/";
}
return preg_replace($pre1[0], $pre2, $string);
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.nl2br.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39615
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39615&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39615&report=yes
Search -- http://master.php.net/manage/user-notes.php