Results 1 to 10 of 10
-
01-16-2007, 06:43 PM #1Aspiring Evangelist
- 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
-
01-16-2007, 08:23 PM #2New Member
- Join Date
- Jan 2007
- Location
- London, England
- Posts
- 3
Originally Posted by jonathanbull
<?
$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...
-
01-16-2007, 09:04 PM #3Web Hosting Master
- 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)) {
PHP Code:$counter = 0;
-
01-16-2007, 09:48 PM #4Retired Moderator
- 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
-
01-16-2007, 09:57 PM #5Web Hosting Evangelist
- Join Date
- Apr 2005
- Posts
- 522
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
-
01-17-2007, 04:25 AM #6New Member
- 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
-
01-17-2007, 09:34 PM #7Aspiring Evangelist
- 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 />
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
-
01-17-2007, 09:52 PM #8Retired Moderator
- 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);
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
-
01-17-2007, 10:10 PM #9Web Hosting Master
- Join Date
- Mar 2006
- Posts
- 984
Now that one is very interesting though. Thanks for posting this foobic.
-
01-17-2007, 10:14 PM #10Aspiring Evangelist
- Join Date
- Aug 2003
- Posts
- 424
Originally Posted by foobic
$message = preg_replace("/\s*$/", '', $message);
Jon