|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-04-12 02:12 UTC] valentiny510 at yahoo dot es
Description:
------------
The name "nl2br" for somebody who doesn't know php very well, suggest that actually replace "nl" with "br" but is not true. The name of the function function should be "nl2nl+br"
I think it should have a third parameter like $replace, and actually Replace the nl with br
I have some clients who used this function inside pre with horrible result.
Anyway, I think it will be more usefull this
nl2br ($string, true/false, $replace = true/false)
than
preg_replace('#([\r?\n]+)#', '<br>', $string) or
str_replace(array("\r\n", "\r", "\n"), '<br>', $string)
Patchesnl2br_additional_parameter (last revision 2013-04-17 19:44 UTC by krakjoe@php.net)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 04:00:01 2025 UTC |
yes would be a shorter and more elegant solution nl2br($str, false, true); nice and clean..In my eyes this is an unnecessary change. nl2br handles the common case, where you want to have linebreaks displayed and have the code nicely formatted at the same time. This is what nl2br does and I don't think it need to do any more. I don't see why we should add support for incorrect usages of the function inside <pre>. And in any case, if you should have need for this (for whatever odd reason), then it is trivial to write yourself. Quite honestly I think that preg_replace('#([\r?\n]+)#', '<br>', $string) is a good bit clearer than nl2br($string, false, true). That's just too many unclear boolean parameters.The bigger issue here is how you'd reverse the process. Without knowing that the function just adds instead of replacing, one would try the following: function br2nl($string, $is_xhtml = TRUE) { return str_replace( $is_xhtml ? '<br />' : '<br>', PHP_EOL ); } The problem is that this would result in doubling the newline characters.