note 39641 added to function.nl2br
| From: | greywyvern-greywyvern-com at rn2 dot php dot net | Date: | Thu, 05 Feb 2004 18:11:28 +0000 |
| Subject: | note 39641 added to function.nl2br | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64696@lists.php.net to get a copy of this message | ||
Just a couple adjustments to the function below.
<?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("/\s*<br[^>]*?>\s*/", "", $pre1[0][$x]);
$pre2[$x] = preg_replace("/([^\n]{".$wrap."})(?!<\/pre>)(?!\n)/",
"$1\n", $pre2[$x]);
$pre1[0][$x] = "/".preg_quote($pre1[0][$x], "/")."/";
}
return preg_replace($pre1[0], $pre2, $string);
}
?>
You might ask, why not just use:
<?php $string = str_replace("\n", "<br />", $string); ?>
... and prevent double spacing that way by actually *replacing* the \n with <br />, whereas
nl2br() *inserts* a <br />.
Well, the answer is, doing it that way makes all the HTML output appear on a single line! Ugly, to
say the least. The function above will keep your HTML source output formatted the same way you
input it. :)
----
Manual Page -- http://www.php.net/manual/en/function.nl2br.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39641
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39641&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39641&report=yes
Search -- http://master.php.net/manage/user-notes.php