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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.