Replace all new lines with <br> and double new lines with <p>?
Results 1 to 7 of 7

Thread: Replace all new lines with <br> and double new lines with <p>?

  1. #1
    Senior Member
    Join Date
    Oct 2003
    Posts
    282

    Replace all new lines with <br> and double new lines with <p>?

    Is it possible to replace any double new lines \n with a paragraph tag... while at the same time as replacing and single new lines \n with just a <br>

    ?

    thanks!
    I <3 PHP Builder!

  2. #2
    Assuming your string is in a variable called $str, use this:
    Code:
    "<p>".str_replace('<br />', '</p><p>', nl2br($str,true))."</p>"
    That should take care of it.

  3. #3
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,902
    Untested:
    PHP Code:
    $formattedText "<p>".preg_replace('/\r?\n/''<br />'preg_replace('/(\r?\n){2,}/'"</p><p>"$originalText))."</p>"
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  4. #4
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    Quote Originally Posted by NogDog View Post
    Untested:
    PHP Code:
    $formattedText "<p>".preg_replace('/\r?\n/''<br />'preg_replace('/(\r?\n){2,}/'"</p><p>"$originalText))."</p>"
    Here's an untested mutation of the above untested code:

    PHP Code:
    $formattedText '<p>' preg_replace( array( '/(\r?\n){2,}/''/\r?\n/' ), array( '</p><p>''<br />' ), $originalText ) . '</p>'

  5. #5
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,845
    the problem is that a single new line is a substring of double new lines. If you do the replacement for double new lines before the single new lines and then run the single new lines replacement, you should be fine.

    In practice, this may get messy if you have new line chars separated by white space (spaces, tabs, etc.). It can also be complicated by the fact that there is the \r carriage return and the \n newline char (see wikipedia article on new line chars) and the fact that most newline files on a windows machine consist of both a \r and a \n character.

    In practice, why do you need the </p> char for double new lines? Is there anything all that wrong with two newline chars in a row (</br></br>) ??
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  6. #6
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,902
    Or perhaps just wrap the output in <pre> tags, and use CSS to format them as desired if you want, say, a different font.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  7. #7
    Senior Member
    Join Date
    Mar 2009
    Posts
    802
    Quote Originally Posted by sneakyimp View Post
    In practice, why do you need the </p> char for double new lines? Is there anything all that wrong with two newline chars in a row (</br></br>) ??
    This is what I do when I run into this kind of situation.
    Declare variables, not war.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •