Re: preg_replace - regex for replace string

From: Date: Wed, 27 Mar 2002 23:17:24 +0000
Subject: Re: preg_replace - regex for replace string
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-90448@lists.php.net to get a copy of this message
On Wed, 27 Mar 2002, James Taylor wrote: > Ok, let me try to refine this as the big picture. User enters something into > a form. > > We take that form value and convert newlines into <br /> though nl2br. > > Then, I want the limit the number of consecutive <br />'s a user can enter, > to avoid abuse via a person just holding down enter for a while. > > $message = nl2br($message); > $message = preg_replace("/\n/", "", $message); > > while (stristr($message, '<br /><br /><br />')) { > $message = str_replace('<br /><br /><br />', '<br > /><br />', $message); > } If you want to keep it to a maximum of, say, 2 <br /> tags, then why not just use something like: $message = preg_replace('/(<br />\w*){2,}/', '<br /><br />', nl2br($message)); miguel

« previous php.general (#90448) next »