treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Convert line breaks to paragraphs

  • 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.
  • This should work I'd imagine:
    <?php
    // $content is whatever the string is which contains the line breaks
    $contentArr = explode( '<br />', $content );

    foreach( $contentArr as $paragraph ) {
    echo '<p>' . $paragraph . '</p>';
    };
    ?>
  • Thanks. Greatly appreciated!
  • actually, since the closing </p> tag is optional, you could just do
    $content = str_replace( '<br>','<p>',$content );

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