|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34381 nl2br() should have an option for XHTML/HTML compatible BR element
Submitted: 2005-09-05 22:56 UTC Modified: 2008-08-14 02:56 UTC
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: jasper at bryant-greene dot name Assigned: kalle (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 5.0.4 OS: N/A
Private report: No CVE-ID: None
 [2005-09-05 22:56 UTC] jasper at bryant-greene dot name
The nl2br() function, as of PHP 4.0.5, always inserts <br />. However, in HTML a complaint parser should understand this as <br>&gt; (it is the SGML SHORTTAG syntax).

For example, this code:

| <p>This is<br />a paragraph</p>

should be rendered as this in an HTML document[1]:

| This is
| >a paragraph

Therefore, in order to maintain compatibility with both HTML and XHTML specifications, I request that an option be added to nl2br() that allows XHTML or HTML compliant br elements to be inserted.

[1] for more information, see


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-25 17:25 UTC] nospam at wanted dot by dot me
That <br /> tags generated by the NL2BR() function are not compliant with an HTML 4.01 DOCTYPE is now commented on otherwise legitimate pages by the W3C validator.

I fail to understand why a new function wasn't invented to handle output intended for XHTML instead of simply amending this function intended for HTML output as of PHP 4.0.5 onwards -  did you assume that everyone would immediately migrate to XHTML at that time?

The only alternative for PHP authors who wish to generate HTML compliant markup at present are left with is to write their own version of NL2BR() and replace it's use throughout their script bases and libraries when generating HTML.

A solution PHP could offer is a new function that enables PHP authors to specify whether an HTML page is being output or whether it is something else, like XHTML or XML. This function (or method) could be invoked at the same time as the DOCTYPE is output. It could also be defaulted by a php.ini switch quite handily.
 [2008-03-13 15:28 UTC] holliwell at gmx dot net
I second jasper's suggestion of an optional switch, if one wants plain old html 4.01 <br>, default should be xhtml-compliant <br />.
 [2008-08-07 01:30 UTC]
Try this patch (for PHP_5_3 cvs):

This adds a new parameter to nl2br() to enable nl2br() to return "<br>" insted of "<br />"
 [2008-08-14 02:56 UTC]
This feature has been implemented and is available as of PHP 5.3
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 14 14:01:29 2024 UTC