Solved

string char replacement

Posted on 1998-10-20
7
213 Views
Last Modified: 2010-03-05
I have a variable $myStory that contains text and line breaks eg:

$myStory = "This is the first line\nthis is the second line\nthis is the thrid\n"

notice the line breaks.

I want print the string char by char, if the char is a "\n" I want to print a "<BR>"

I tried to convert all the \n to a <BR>'s with the following code but doesn't work with more that one charater:

$myStory =~ tr/\n/"<BR>"/;

can anybody help me out?
0
Comment
Question by:georgep23
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1205530
$myStory =~ s/\n/<BR>/g;
0
 
LVL 1

Expert Comment

by:development
ID: 1205531
have you tried
$line=~ s/\\n/<BR>/ig;
The g option on the end is global, so it does more than one in the line.  Ignore the $line, thats me being lazy.
0
 
LVL 1

Author Comment

by:georgep23
ID: 1205532
ozo, cool, worked first time,

can never get my head around those search string things!


0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
LVL 4

Expert Comment

by:martinag
ID: 1205533
I'd do
$myStory =~ s/\n/\n<BR>/g;
which will make the source easier to read.

Points go to ozo though. As usual... :-)

Martin
0
 
LVL 1

Author Comment

by:georgep23
ID: 1205534
martinag,
I already did this ;)))


ozo can you repost as answer

thanks for your help everybody!
0
 
LVL 4

Expert Comment

by:martinag
ID: 1205535
>> ;)))
Are those )s your double chins? ;-)

Martin
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 1205536
$myStory =~ s/\n/<BR>\n/g;
# see
# perldoc perlre
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…

792 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