RealNl2Br

Hence nl2br does NOT really replace newlines with <br /> tags (it only inserts a <br /> tag before each \n → newlines themselves are preserved), you can use this function to get it right (realNl2Br):

/**
 * @param $string - string with
 * @param bool $xhtml - deliver xhtml confom br's or not (<br /> or <br>)
 * @return mixed - string with br's
 */
public function realNl2br($string, $xhtml = true){
    $replace = '<br />';
    if(!$xhtml){
        $replace = '<br>';
    }
    return str_replace( "\n", $replace, $string);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.