Jump to content

[SOLVED] nl2br and multiple line breaks


Prodigal Son

Recommended Posts

Hmm... weird. I used this to prevent user input from creating big spaces and I tried this and it worked nicely preventing multiple line breaks. But for some reason a user was still able to create more than 2 line breaks. I tried copying and pasting their message with lots of line breaks, but when I did it, it got cut down to 2... so I have no idea how that individual did that. Anyone see why the individual could do that?

Either their post was already IN the database...or you don't use htmlspecialchars() and they just used a straight <br /> tag.

Hmm, nope their post wasn't in the database yet. I do use htmlspecialchars so the br tag doesn't work. So I find it weird how they did that since I even copy and pasted their input and mine got reduced.

Either their post was already IN the database...or you don't use htmlspecialchars() and they just used a straight <br /> tag.

After playing around for a bit I seem to have found out how they did it.

For each space character you make after pressing enter it creates a breakline.

For example if you type Hello followed by the enter key 5 times and Goodbye, you'd get Hello, 2 breaklines, then Goodbye.

But if you type Hello, press enter, press space, press enter, press space, press enter, press enter, and type goodbye. You can get 3 line gaps in between.

 

Haven't used regex in a long time, and I was never that good with it to begin with, but I tried this:

$str = preg_replace("/(\\s){2,}/s"," ",$str);
$str = preg_replace("/(\\n){3,}/s","\n\n",$str);

 

But it just ended up removing all breaklines and extra spaces instead. Anyone see what's wrong?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.