Solved

PHP/REGEX: Replace multiple <br /> tags with a single <br /> tag

Posted on 2014-02-10
6
2,817 Views
Last Modified: 2014-02-11
Using PHP and Regular Expressions, how can I replace multiple <br /> tags with a singe <br /> tag?

For example, this:
<br>
<br /><br>    <br />  <br/>
<br />


Should become this:
<br />
0
Comment
Question by:hankknight
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39848525
Try this:
$string = preg_replace("#(<br\s*/?>\s*){2,}#","<br />",$string);

Open in new window


\s matches any space character, which can be a space, tab, line feed or new line.
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39848794
You don't even need the {2,} at the end; you can just + it and that would ensure that all <br> tags are W3C compliant. :-)

I've also modified @Terry's regex to account for newlines:

$string = preg_replace("#(<br\s*/?>(\r|\n|\s|\t)*)+#","<br />",$string);

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39848804
Thanks Derek, a + is simpler, and does replace single occurrences of a non-standard <br> tag with the standard <br /> one. The newlines were already covered by \s though, so that change is unnecessary.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Assisted Solution

by:Derek Jensen
Derek Jensen earned 200 total points
ID: 39848815
Interesting, I've always had difficulty with \s finding newlines unless I used the m or s modifier; I'll have to do some more testing...

...Indeed you are right! :-)  ya learn somethin' new every day! ;-)
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 300 total points
ID: 39848827
The s modifier allows the . wildcard to also match a newline (\n) character.
The m modifier makes the ^ and $ characters match the start and end of every line (with regard to \n characters) rather than just the start and end of the entire string.

Combining our improvements, the result is:
$string = preg_replace("#(<br\s*/?>\s*)+#","<br />",$string);

Open in new window

0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39848834
Yeah, I always get those two mixed up; thanks for the clarification! :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now