note 53565 added to function.nl2br

From: Date: Mon, 06 Jun 2005 09:00:12 +0000
Subject: note 53565 added to function.nl2br
Groups: php.notes 
Request: Send a blank email to php-notes+get-89995@lists.php.net to get a copy of this message
As stated in the manual above, PHP's nl2br() feature only puts a "<br />" tag before each newline ("\n"). So -- if you intend to code a br2nl() function for yourselves, all you have to do is remove every occurence of "<br />" or "<br>". Rather than get into nasty regular expressions to accomplish this, just use what PHP has built in already -- str_replace(): <?php /* br2nl for use with HTML forms, etc. */ function br2nl($text) { /* Remove XHTML linebreak tags. */ $text = str_replace("<br />","",$text); /* Remove HTML 4.01 linebreak tags. */ $text = str_replace("<br>","",$text); /* Return the result. */ return $text; } ?> The final result from this function being called is whatever was entered before XHTML/HTML linebreaks were added. All newlines are preserved by default, as per PHP ln2br() specification. Since the code above preserves newlines also, you can expect your data to reappear in the same way it was entered. Hope this helps. ---- Manual Page -- http://www.php.net/manual/en/function.nl2br.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+53565 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53565&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53565&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53565&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53565&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53565&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+53565&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89995) next »