[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2014-02-10
6
Medium Priority
?
3,113 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Assisted Solution

by:Derek Jensen
Derek Jensen earned 800 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 1200 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

650 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