?
Solved

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

Posted on 2014-02-10
6
Medium Priority
?
3,036 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

801 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