• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

string char replacement

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
georgep23
Asked:
georgep23
  • 2
  • 2
  • 2
  • +1
1 Solution
 
ozoCommented:
$myStory =~ s/\n/<BR>/g;
0
 
developmentCommented:
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
 
georgep23Author Commented:
ozo, cool, worked first time,

can never get my head around those search string things!


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
martinagCommented:
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
 
georgep23Author Commented:
martinag,
I already did this ;)))


ozo can you repost as answer

thanks for your help everybody!
0
 
martinagCommented:
>> ;)))
Are those )s your double chins? ;-)

Martin
0
 
ozoCommented:
$myStory =~ s/\n/<BR>\n/g;
# see
# perldoc perlre
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now