Solved

string char replacement

Posted on 1998-10-20
7
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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

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!

Question has a verified solution.

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

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.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

739 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