php - Preg Match multiple br tags -
php - Preg Match multiple br tags -
i have textarea users come in comments , using nl2br allow them add together spaces between blocks of text. replace occurrences 3 or more br tags appear in row , replace them single tag.
one or 2 br tags don't replaced they're fine anymore more needs replaced single tag.
this regular look have far
$comment = preg_replace('/(<br \/>){3,}/', '<br />', $comment);
$comment variable is
one<br /> <br /> <br /> <br /> <br /> two<br /> <br /> <br /> <br /> <br /> three<br /> <br /> 4
changing regex
$comment = preg_replace('/(<br \/>)/', '-', $comment);
replaces br tags hyphens, seems it's {3,} i'm not sure.
you can utilize next regular expression. since repeating tags on same line or separated newline sequence, need business relationship whitespace.
$comment = preg_replace('~(?:<br />\s*){3,}~', '<br />', $comment);
regex explanation | code demo
if logical reason removes whitespace want retained, utilize ...
$comment = preg_replace('~(?:<br />\r?){3,}~', '<br />', $comment);
php regex
Comments
Post a Comment