Forum Moderators: coopster

Message Too Old, No Replies

Stopping multiple <br />'s using PHP

How do I stop people from inputting multiple <br />'s in PHP?

         

666admin

8:04 pm on Sep 25, 2011 (gmt 0)

10+ Year Member



This is what I'm currently using and it only works if someone uses 4 <br />'s, they can bypass it by using 5+ <br />'s.

$replace = array("\r\n","\n\r","\r","\n");
$message = str_replace($replace, '<br />', $message);
while(strstr($message, "<br /><br /><br /><br />"))
{
$message = str_replace("<br /><br /><br /><br />", '<br /><br /><br />', $message);
}

penders

10:12 pm on Sep 25, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you are better off using a regex. The following will substitute sequences of 4 or more (whatever kind of) newlines for 3 newlines (line feeds).

$message = preg_replace('/(\r\n){4,}|\n{4,}|\r{4,}/', "\n\n\n", $message);


Unless you need the "<br />" earlier, you can replace with "<br />" later for display if you wish using nl2br().

666admin

1:41 am on Sep 26, 2011 (gmt 0)

10+ Year Member



Thank you very much, penders!

Appreciated.