A Better Alternative to PHP nl2br

This article explores different options for preserving paragraph formatting in PHP. It will show you how to display entered text in a textarea properly without the need to use the PHP nl2br function.

HTML ignores whitespace, including new lines. In order to display paragraphs properly and not in a continuous block (for example when saving text from a texarea or retrieving it from a database) you have the following options:

  • Save text with its HTML.
  • Change new lines to <br> tags using nl2br().
  • Replace new lines with paragraph tags by creating a custom function.

The Simplest way is to use the nl2br() function. However, It’s not semantic to use line breaks instead of paragraph,. In other words, it makes fake paragraphs. Furthermore, nl2br() function inserts a slash before the closing angle bracket for compatibility with XHTML. Although it’s still compatible with HTML5, I’d prefer to use <br> than <br />. It’s a quick and dirty solution, but not ideal.

In order to save text with its HTML you can use an HTML editor like TinyMCE or CK Editor and then store the result. It’s a better option than nl2br(), but still not ideal.

I wasn’t satisfied with any of these options, so I decide to write my own php function to do the conversion. To use this function you need to pass the text retrieved from your database(or any other source) to the convert() function. It reads the text and whenever it encounters a newline character it wraps up the text in paragraph without adding any
tags.

Here is the code:

function convert ($text) {
$text = trim($text);
return '<p>' . preg_replace('/[\r\n]+/', '</p><p>', $text) . '</p>';
}

Use it like this:

$text = 'paragraph one.
paragraph two.';
echo convert($text);

3 comments

  1. Jay - July 11, 2013 4:55 am

    Good job mate, solved a problem i had with base64_encode on wordpress posts (don’t ask its a strange project).

    Reply
  2. daguru69 - February 20, 2015 8:57 pm

    this is great…nl2br is the stupidest thing ever. So many programmers on Stack Overflow are raving about it, but it’s dumb. There’s no reason to put so many tags in a file just to keep proper formatting. Thanks for thinking outside the box instead of following the herd.

    Reply
  3. immeëmosol - July 24, 2015 2:34 pm

    Suggestion :

    if ( !function_exists( ‘nl2p’ ) ) {
    function nl2p ( $text ) {
    return ” . str_replace( array( “\r\n” , “\r” , “\n” ) , ” , $text ) . ”;
    }
    }

    Reply

Have your say