Solved

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

Posted on 2014-02-10
6
2,769 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

13 Experts available now in Live!

Get 1:1 Help Now