New line to paragraph function

Here is another approach that doesn't use regular expressions. Note, this function will remove any single line-breaks.

function nl2p($string)
{
    $paragraphs = '';

    foreach (explode("\n", $string) as $line) {
        if (trim($line)) {
            $paragraphs .= '<p>' . $line . '</p>';
        }
    }

    return $paragraphs;
}

If you only need to do this once in your app and don't want to create a function, it can easily be done inline:

<?php foreach (explode("\n", $string) as $line): ?>
    <?php if (trim($line)): ?>
        <p><?=$line?></p>
    <?php endif ?>
<?php endforeach ?>

The problem is with your match for single line breaks. It matches the last character before the line break and the first after. Then you replace the match with <br>, so you lose those characters as well. You need to keep them in the replacement.

Try this:

function nl2p($string, $line_breaks = true, $xml = true) {

$string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);

// It is conceivable that people might still want single line-breaks
// without breaking into a new paragraph.
if ($line_breaks == true)
    return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'), trim($string)).'</p>';
else 
    return '<p>'.preg_replace(
    array("/([\n]{2,})/i", "/([\r\n]{3,})/i","/([^>])\n([^<])/i"),
    array("</p>\n<p>", "</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'),

    trim($string)).'</p>'; 
}

@Laurent's answer wasn't working for me - the else statement was doing what the $line_breaks == true statement should have been doing, and it was making multiple line breaks into <br> tags, which PHP's native nl2br() already does.

Here's what I managed to get working with the expected behavior:

function nl2p( $string, $line_breaks = true, $xml = true ) {

    // Remove current tags to avoid double-wrapping.
    $string = str_replace( array( '<p>', '</p>', '<br>', '<br />' ), '', $string );

    // Default: Use <br> for single line breaks, <p> for multiple line breaks.
    if ( $line_breaks == true ) {
        $string = '<p>' . preg_replace(
            array( "/([\n]{2,})/i", "/([\r\n]{3,})/i", "/([^>])\n([^<])/i" ),
            array( "</p>\n<p>", "</p>\n<p>", '$1<br' . ( $xml == true ? ' /' : '' ) . '>$2' ),
            trim( $string ) ) . '</p>';

    // Use <p> for all line breaks if $line_breaks is set to false.
    } else {
        $string = '<p>' . preg_replace(
            array( "/([\n]{1,})/i", "/([\r]{1,})/i" ),
            "</p>\n<p>",
            trim( $string ) ) . '</p>';
    }

    // Remove empty paragraph tags.
    $string = str_replace( '<p></p>', '', $string );

    // Return string.
    return $string;

}

I also wrote a very simple version:

function nl2p($text)
{
    return '<p>' . str_replace(['\r\n', '\r', '\n'], '</p><p>', $text) . '</p>';
}

Tags:

Php

Regex