Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-10-27
4
Medium Priority
?
1,526 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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

876 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