note 56737 added to function.nl2br

From: Date: Wed, 14 Sep 2005 06:15:06 +0000
Subject: note 56737 added to function.nl2br
Groups: php.notes 
Request: Send a blank email to php-notes+get-95138@lists.php.net to get a copy of this message
I wrote this because I wanted users to be able to do basic layout formatting by hitting enter in a textbox but still wanted to allow HTML elements (tables, lists, etc). The problem was in order for the output to be correct with nl2br the HTML had to be "scrunched" up so newlines wouldn't be converted; the following function solved my problem so I figured I'd share. <?php function nl2br_skip_html($string) { // remove any carriage returns (mysql) $string = str_replace("\r", '', $string); // replace any newlines that aren't preceded by a > with a <br /> $string = preg_replace('/(?<!>)\n/', "<br />\n", $string); return $string; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.nl2br.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56737 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56737&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56737&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56737&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56737&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56737&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56737&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56737&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95138) next »