php / php/php-src

Replace the newlines in nl2br()

Open
#15,845 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Extension: standard Feature Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

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

  • nl2br() would be idempotent when 3rd parameter is set to true. Today n2br() cannot be applied several times to a string without managing extra inserts.
  • Simplify the code. We could remove the wrappers to replace the newlines from our code.
  • Flexibility. Old behavior could be kept.
  • Backward compatible.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the nl2br() implementation and its tests in php-src, then compare the current insertion behavior with the proposed replace parameter. Done means the third parameter preserves existing behavior by default and replaces newline characters when enabled, with coverage for both modes.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.