tags:

views:

61

answers:

2

Hi this is fairly simple

I want to know how to use nl2br(); in php, but limit the amount of consecutive <br/>'s that are allowed at one time.

//For Example: A user enters

1st line

2 line breaks



3 line breaks





6 line breaks


2 line breaks




Is there anyway to have php limit the <br/>'s to no more than x amount of numbers at at time

so if we only allowed 4 <br>'s at a time the output would be

1st line

2 line breaks



3 line breaks




6 line breaks (changed to 4)


2 line breaks
+1  A: 

Try a regular expression to limit sequential newlines before passing things into nl2br:

$clean = preg_replace('/\n{4,}/', '\n', preg_replace('/\r/', '', $dirty));

For the uninitiated:

  • \r is a Carriage Return, which we can safely strip before calling nl2br
  • \n is a Newline
  • {4,} means "repeat the last character four or more times

So, any sequence of four or more newlines in a row will be reduced to one.

Unfortunately this is easily defeated. A user can simply come by and hit enter + space + enter + space + enter + space...

It's a start, though. Let's make it a bit more idiot-proof.

$dirty = preg_replace('/\r/', '', $dirty);
$clean = preg_replace('/\n{4,}/', '\n', preg_replace('/^\s+$/m', '', $dirty));

Now we're also:

  • Setting the "m" modifier so...
  • The ^ means "the beginning of a line" (without "m" it means the beginning of the entire string)
  • The $ means "the end of a line" (without "m" it means the end of the entire string)
  • \s+ means "any space character, repeating"

So, now it removes \r, then changes lines that are nothing but spaces to being empty, then it limits empty lines.

Beware, this may break formatting, depending on what markup you're using. Certain markup engines can operate on lines that are pure-whitespace and do smart things with them, such as the markup engine used here at SO.

Have you considered banning jerks? It's sometimes effective. :)

Charles
Thanks so much for you response, its great people actually help on here. I will try this now.
kr1zmo
I've edited in an alternative, and a caveat. :)
Charles
In your script above where would i impliment my posted data, $_POST['fb_msginput'])
kr1zmo
Btw no formatting is allowed its just a comments box being posted
kr1zmo
That'd be $dirty.
Charles
Charles this is converting all lines to 2 brs? It was suposed to allow anything under 4 lines, but if its over 4 then just show 4.
kr1zmo
Replace the '\n' as the second param to preg_replace with '\n\n\n\n'
Charles
so $clean = preg_replace('/\n{4,}/', '\n\n\n\n', preg_replace('/^\s+$/m', '', $dirty));
kr1zmo
Thanks charles but idk, i cant get it to work.
kr1zmo
Thanks i got it all i had to do was replace $clean = preg_replace('/\n{4,}/', '<br/><br/><br/><br/>', preg_replace('/^\s+$/m', '', $dirty)); Php is now changing anything over 4 line breaks to 4 line breaks
kr1zmo
I'm glad you got it working. :) If you feel my answer was best, click the green checkmark in the left column.
Charles
A: 

Got it guys, this wont allow anymore than x amount of line breaks at a time.

$string = the input where modifying
$num = the amount of line breaks we wont allow the user to go over

function nl2br_limit($string, $num){

    return preg_replace('/\n/', '<br/>', preg_replace('/(\s{'.$num.'})\s+/','$1', $string));
    }

kr1zmo