View Full Version : Replace 2 <br />s with \n

03-03-2007, 07:30 AM
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?

03-03-2007, 02:07 PM
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.

03-04-2007, 06:06 PM
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.