Solved

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

Posted on 2014-02-10
6
2,916 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 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