Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2003
    Posts
    424

    [PHP] Removing 3 or more occurences of <br /> ?!

    Hi there,

    I am trying to write a script to stop people abusing my comment form. Here's the problem, people have been adding loads of <br/>'s to the end of their messages in order to screw the formatting of the comment box up.

    Is there a sure fire way of stopping this happening? I'd still like the <br/> tag to be usable as its always nice for the (legitimate) users to be able to space out their comment.

    If anybody could throw some tips some way I'd be extremely grateful - I'm bamboozled!


    Thank you in advance,
    Jon

  2. #2
    Join Date
    Jan 2007
    Location
    London, England
    Posts
    3
    Quote Originally Posted by jonathanbull
    Hi there,

    I am trying to write a script to stop people abusing my comment form. Here's the problem, people have been adding loads of <br/>'s to the end of their messages in order to screw the formatting of the comment box up.

    Is there a sure fire way of stopping this happening? I'd still like the <br/> tag to be usable as its always nice for the (legitimate) users to be able to space out their comment.

    If anybody could throw some tips some way I'd be extremely grateful - I'm bamboozled!


    Thank you in advance,
    This is something I just threw together in about a minute, so sorry if it's not the best way to go about this...

    <?
    $string = "Hello <br /> Simon <br><br /><br>";
    $counter = 0;
    for ($i = 0; $i < strlen($string); $i++){
    if (($string[$i] == "<" && $string[$i + 1] == "b" && $string[$i + 2] == "r" && $string[$i + 3] == " " && $string[$i + 4] == "/" && $string[$i + 5] == ">") || ($string[$i] == "<" && $string[$i + 1] == "b" && $string[$i + 2] == "r" && $string[$i + 3] == ">")){
    $counter++;
    }
    }
    echo "There are " . $counter . " line breaks in this string";
    ?>

    Obviously this is just a starting point. Now you know how to find the amount of line breaks in a string...

  3. #3
    Join Date
    Mar 2006
    Posts
    984
    Looks like a very interesting codings above. However, I'd add:

    PHP Code:
    if (preg_match("/^<br />|<br>/i"$string)) { 
    below:

    PHP Code:
    $counter 0
    Then, add an ending bracket below the other two in the codes.

  4. #4
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,849
    Wow.

    Ok, try this:
    PHP Code:
    $comment preg_replace('#(<\s*br[^/>]*/?\s*>\s*){2,}#is',"<br />\n",$comment);
    // Replace 2 or more breaks in a multi-line string with a single break. 
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  5. #5
    What you need is this: replace multiple \n (newlines), then convert them to <br> (or <br />. That's easily done via regular expressions. Assuming you have your contact message stored in $message variable, this is the code:
    PHP Code:
    $message str_replace("\r"''$message); // removes carriage return
    $message preg_replace("#(\n){2,}#""\\1"$message); // replaces newlines with only one newline if there are more than 2 in a row
    $message nl2br($message); // convert your \n to <br /> 
    Dyslexics Have More Fnu

  6. #6
    Join Date
    Jan 2007
    Location
    London, England
    Posts
    3
    Erm...wow!

    Re-reading my code this morning...ouch. Remind me never to do 14 hour coding stints ever again! Seems to be frying my brain. O_O

  7. #7
    Join Date
    Aug 2003
    Posts
    424
    Thank you so much simonmaddox, horizon, foobic and maxymizer - I checked back on this thread this morning and never thought there would be so many useful responses!

    I went with simonmaddox's code, it seems to be the most reliable in my situation:
    PHP Code:
    $message str_replace("\r"''$message); // removes carriage return
    $message preg_replace("#(\n){2,}#""\\1"$message); // replaces newlines with only one newline if there are more than 2 in a row
    $message nl2br($message); // convert your \n to <br /> 
    Can I ask how to make one addition? At the moment this code replaces any more than 2 newlines with just the one newline. This is perfect.

    However, how can I make it so that no newlines whatsoever are allowed at the very end of the message?


    For example:

    ----------------------
    testtext



    testtext








    testtext





    ------------------


    would become:


    ------------------
    testtext

    testtext

    testtext
    ------------------


    I hope I've made enough sense! Thank you all so much for your replies once again - I've learnt so much.
    Jon

  8. #8
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,849
    Actually, that was maxymizer's code - a good choice if what you have coming from the form are newlines and not (x)html breaks.

    To take out any trailing newlines and other whitespace you could use a similar regexp:
    PHP Code:
    $message preg_replace("/\s*$/"''$message); 
    or the built-in function rtrim()
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  9. #9
    Join Date
    Mar 2006
    Posts
    984
    Now that one is very interesting though. Thanks for posting this foobic.

  10. #10
    Join Date
    Aug 2003
    Posts
    424
    Quote Originally Posted by foobic
    Actually, that was maxymizer's code
    Oops - my mistake. Sorry maxymizer and thank you once again.

    $message = preg_replace("/\s*$/", '', $message);
    Works perfectly - thanks so much!

    Jon

Posting Permissions

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