php / php-src

The PHP Interpreter
https://www.php.net
Other
38.32k stars 7.76k forks source link

Replace the newlines in nl2br() #15845

Open fredericch opened 2 months ago

fredericch commented 2 months ago

Description

The function name of nl2br() suggests that it is a "replace" operation but is actually an "insert" operation.

Would it be possible to add a 3rd parameter so the function would actually replace the newline characters?

nl2br(string $string, bool $use_xhtml = true, bool $replace = false): string

The confusion with the name and the need for the 3rd parameter is obvious in the user contributed notes (most popular one and following ones): https://www.php.net/manual/en/function.nl2br.php#49516

The advantages of the 3rd parameter (instead of changing the behavior):

cmb69 commented 2 months ago

In my opinion, nl2br() is a blunt tool to quickly convert some plain text into semantically suboptimal HTML (e.g. two consecutive newlines are better represented as a "paragraph break"). I would leave the function as is, especially since it is easy to do your own conversion using str_replace(), for instance.

And I'm really not keen on adding a boolean parameter anyway; these are okayish when used as named parameters, but still not quite right, in my opinion.

devnexen commented 2 months ago

kind of in a same boat, if there were no other choice then yes but as you demonstrated yourself, you can manage without.

fredericch commented 2 months ago

So would it make sense to change the behavior? Do a real "nl-2-br" operation and not a "nl-add-br"? :)

That way the function name would be aligned with its behavior.

devnexen commented 2 months ago

I would suggest, as a next step, to write to the internal mail list.