Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

substitute an enter with regular expressions

by Anonymous Monk
on Jan 15, 2001 at 22:11 UTC ( [id://51984]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I get an input string of several lines and I need to insert it into a data base. In order to do so I need to change all the newlins into br tags. Need your help. Thanks. Tsvika.
  • Comment on substitute an enter with regular expressions

Replies are listed 'Best First'.
Re: substitute an enter with regular expressions
by Fastolfe (Vicar) on Jan 15, 2001 at 22:17 UTC
    I think we may need more context from your post. Define "newline". Are we talking about a legitimate newline from a native text file on your OS, or something provided from a web browser (as might be seen by a multi-line input field in a CGI script, which is what I suspect is the case here). If it's the former, s/\n/<br>/g will do the job. If it's the latter, in my experience, line breaks here tend to be encoded using an ASCII CR, not a "newline". In that case, something like s/\cM/<br>/g is probably what you want. YMMV.
For readability...
by tedv (Pilgrim) on Jan 15, 2001 at 23:11 UTC
    Since you're putting real newline tags in, it's quite possible that other people might look at these entries. With that in mind, your substitute should keep things readable. Suppose my input looks like this:
    Foo Bar Foobar

    Then the actual string is "Foo\nBar\nFoobar\n". If I just ran a simple s/\n/<br>/g on the string, I'd get this output:
    Foo<br>Bar<br>Foobar<br>

    Clearly this would get illegible after 20 or so entries. So use a subtitute like this instead: s/\n/<br>\n/g
    Your output should look like this:
    Foo<BR> Bar<BR> Foobar<BR>


    -Ted
Re: substitute an enter with regular expressions
by JP Sama (Hermit) on Jan 16, 2001 at 00:33 UTC
    instead using a 'br' in each line, you could print the tag 'pre' and put the whole data in it, that way you would save some bytes in your dbase.

    IMHO of course!
    #!/jpsama/bin/perl
    
Re: substitute an enter with regular expressions
by lemming (Priest) on Jan 15, 2001 at 22:17 UTC
    If your input string is like so: "one\ntwo\nthree" then all you need is s/\n/<BR>/g;
    if you're reading it in a line at a time, you can chomp and join. (Or not chomp and sub and join)
Re: substitute an enter with regular expressions
by Anonymous Monk on Jan 16, 2001 at 01:05 UTC
    The good ole oneliner approach: $myString =~ s/\n/
    /g;
Re: substitute an enter with regular expressions
by mrmick (Curate) on Jan 15, 2001 at 22:17 UTC
    Here's a quick one (untested):
    my $char = '<BR>'; s/\n/$char/g;

    Mick

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://51984]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found