php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64639 Add third parameter to nl2br
Submitted: 2013-04-12 02:12 UTC Modified: 2015-01-04 22:30 UTC
Votes:3
Avg. Score:3.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: valentiny510 at yahoo dot es Assigned: ajf (profile)
Status: Wont fix Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: valentiny510 at yahoo dot es
New email:
PHP Version: OS:

 

 [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)



Patches

nl2br_additional_parameter (last revision 2013-04-17 19:44 UTC by krakjoe@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-17 19:44 UTC] krakjoe@php.net
The following patch has been added/updated:

Patch Name: nl2br_additional_parameter
Revision:   1366227875
URL:        https://bugs.php.net/patch-display.php?bug=64639&patch=nl2br_additional_parameter&revision=1366227875
 [2013-05-03 07:01 UTC] octavianmarinescu at tavi dot ro
yes would be a shorter and more elegant solution

    nl2br($str, false, true);

nice and clean..
 [2013-05-03 08:13 UTC] nikic@php.net
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.
 [2013-07-13 01:33 UTC] valentiny510 at yahoo dot es
Nikic, 2 booleans parameters for you are "too many unclear boolean parameters"
LOL...
 [2013-07-13 01:42 UTC] valentiny510 at yahoo dot es
As additional note..
If I can have linebreaks and nicely formated text in just one function, why don't do'it ?
I don't think is such a big deal, or should change the name to something like "addbr2nl", it will be more "correct"..
 [2014-03-06 12:30 UTC] narf at devilix dot net
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.
 [2015-01-04 20:26 UTC] ajf@php.net
Like Nikita, I don't see the point here. Within a <pre> you don't need to use nl2br(). Outside of a <pre>, you need to use nl2br(). Either way, the new-lines will be kept and your source code will be readable.

The only argument for this would be efficiency. But it's not a massive saving, it's 20% off... but only for line endings.
 [2015-01-04 20:30 UTC] ajf@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: ajf
 [2015-01-04 20:30 UTC] ajf@php.net
For the reason stated before, I'm closing this. There's little worth in adding this, nor is it likely to ever happen.
 [2015-01-04 22:30 UTC] nikic@php.net
-Status: Closed +Status: Wont fix
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 19:01:28 2024 UTC