Solved

replace <br>, <br/> and <br /> by \n

Posted on 2003-10-27
4
1,217 Views
Last Modified: 2006-11-17
How to replace - in one line - every <br>, <br/> and <br /> in line by \n.

Replace:

$test = "this is<br>working<br />example<br/>replacement!";

to

$test = "this is\nworking\nexample\nreplacement!";
0
Comment
Question by:weversbv
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:ultimatemike
ID: 9627770

This'll replace each possible case with a newline.


 use strict;

 $_ = "a<br>b<br/>c<br />";

 s/<br>|<br\/>|<br \/>/\n/g;

 print;
0
 
LVL 2

Accepted Solution

by:
ultimatemike earned 125 total points
ID: 9627814
Actually, that regular expression could be changed to:

It says "a <br" followed by 0 or more spaces, then an optional slash, then a >"

s/<br\s*\/?>/\n/g;
0
 
LVL 3

Expert Comment

by:BioI
ID: 9627845
This should work.  Don 't forget to add the "g" in the substitution for multiple substitution and "i" to make it case insensitive [i assume sometimes you will find <BR> instead of <br>?]..

#!/usr/bin/perl -w
use strict;

my $test = "this is<br>working<br />example<br/>replacement!";
$test =~ s/<br>|<br\/>|<br \/>/\n/ig;
print "test --> ", $test, "\n";

# output
test --> this is
working
example
replacement!

0
 
LVL 3

Expert Comment

by:BioI
ID: 9627858
sorry, did some cross posting :-S.  Mike was too fast ;-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

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

14 Experts available now in Live!

Get 1:1 Help Now