Solved

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

Posted on 2003-10-27
4
1,252 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

825 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