Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2005
    Thanked 0 Times in 0 Posts

    Replace 2 <br />s with \n

    I currently accept user input so they can create profiles, and have some information about themselves.

    I'm running the nl2br function on the input, so it is viewed correctly when echoed.

    I'm using str_replace to switch it back to \n (since i strip all <'s and >'s in their input), but the \n's are twice the spacing of the <br />'s

    When they edit their profile, I would like to reverse the process, but only show one \n per every two <br />'s, even if they are on separate lines.

    How would I do that?

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Denver, Colorado USA
    Thanked 340 Times in 332 Posts
    Check the definition of nl2br. You will find that it inserts a <br /> and that it leaves the \n in place. When you replace the inserted <br /> with a \n, you end up with two \n. This is why you have the mistaken impression that a \n is twice the spacing of a <br />.

    The best way to handle this is to store the \n in the input the way that it was entered and only use nl2br function when you output something. This will also slightly lower your storage requirements.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New to the CF scene
    Join Date
    Dec 2006
    Thanked 0 Times in 0 Posts
    I believe you should save you text with /n, and when you display the texts, echo them before using nl2br() function, so you will have no problem.


    Posting Permissions

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