New line to paragraph function

前端 未结 7 1116
情话喂你
情话喂你 2021-01-31 09:20

I have this interesting function that I\'m using to create new lines into paragraphs. I\'m using it instead of the nl2br() function, as it outputs better formatted

7条回答
  •  青春惊慌失措
    2021-01-31 09:51

    Expanding upon @NaturalBornCamper's solution:

    function nl2p( $text, $class = '' ) {
        $string = str_replace( array( "\r\n\r\n", "\n\n" ), '

    ', $text); $string = str_replace( array( "\r\n", "\n" ), '
    ', $string); return '' . $string . '

    '; }

    This takes care of both double line breaks by converting them to paragraphs, and single line breaks by converting them to

提交回复
热议问题