
|
View Full Version : [PHP] Removing 3 or more occurences of <br /> ?!
jonathanbull 01-16-2007, 06:43 PM 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,
simonmaddox 01-16-2007, 08:23 PM 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...
horizon 01-16-2007, 09:04 PM Looks like a very interesting codings above. However, I'd add:
if (preg_match("/^<br />|<br>/i", $string)) {
below:
$counter = 0;
Then, add an ending bracket below the other two in the codes. ;)
foobic 01-16-2007, 09:48 PM Wow. :eek:
Ok, try this:
$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.
maxymizer 01-16-2007, 09:57 PM 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:
$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 />
simonmaddox 01-17-2007, 04:25 AM 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
jonathanbull 01-17-2007, 09:34 PM 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:
$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.
foobic 01-17-2007, 09:52 PM 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:
$message = preg_replace("/\s*$/", '', $message);
or the built-in function rtrim() (http://www.php.net/rtrim)
horizon 01-17-2007, 10:10 PM Now that one is very interesting though. Thanks for posting this foobic. :)
jonathanbull 01-17-2007, 10:14 PM 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!
:)
|