Solved

string char replacement

Posted on 1998-10-20
7
187 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now