Grow your CSS skills. Land your dream job.

Convert line breaks to paragraphs

  • # May 30, 2012 at 11:32 am

    I am accessing data from a MySQL database and have a longtext field which I am echoing to the user.
    However, I want to convert the line breaks to paragraphs. Any ideas on how do I do this?
    Thanks.

    # May 30, 2012 at 12:14 pm

    This should work I’d imagine:

    < ?php
    // $content is whatever the string is which contains the line breaks
    $contentArr = explode( '
    ', $content );

    foreach( $contentArr as $paragraph ) {
    echo '

    ' . $paragraph . '

    ';
    };
    ?>
    # May 30, 2012 at 12:18 pm

    Thanks. Greatly appreciated!

    # May 31, 2012 at 11:17 am

    actually, since the closing </p> tag is optional, you could just do

    $content = str_replace( '
    ','

    ',$content );

    saves a few opcodes.
    jamy_za’s solution works as well, of course.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".