Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Converting carriage returns to HTML breaks

by htmanning (Friar)
on Apr 15, 2012 at 23:58 UTC ( [id://965210]=perlquestion: print w/replies, xml ) Need Help??

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

Monks, I have a mail form. When it is submitted the $message field contains carriage returns that make it into the database as carriage returns, but when the email gets sent the message field is run together. I'm reading it in with:
$message=$in{'message'};
Then doing one of these. I've tried them all:
$message =~ s/\n/\<br\>/g; $message =~ s/\n/<br>/g; $message =~ s/\n/<br\>/g;
None of them work. The source of the email shows white space between paragraphs, but because it's an HTML email the lines are run together when the user reads it. I need to convert those spaces to <br>. What am I missing?

Replies are listed 'Best First'.
Re: Converting carriage returns to HTML breaks
by tinita (Parson) on Apr 16, 2012 at 00:59 UTC
    General hint: If you're not sure what's in your data, print it.
    use Data::Dumper; local $Data::Dumper::Useqq = 1; # will also show you \r, \n and other +things print Dumper $string;
    If you actually see what you've got it's much easier to handle it =)
    in HTML forms you usually get \r\n.
Re: Converting carriage returns to HTML breaks
by chromatic (Archbishop) on Apr 16, 2012 at 00:38 UTC

    Are you certain they're carriage returns? That's \r, not \n.

    You might need to post some example input and output otherwise.


    Improve your skills with Modern Perl: the free book.

      Well, maybe I'm not certain. I hit the Return key in the email form. In all the other scripts I've used /n and it worked fine. I've tried /n and /r and neither work. I'm stumped. Here's what I'm doing.
      Message field as it appears in database: This is a test message. This is line 2 of test message. #This is how I'm getting it from the database... $SQL="SELECT * FROM table WHERE name = '$name'"; &Do_SQL; $pointer = $sth->fetchrow_hashref; $message = $pointer->{'message'}; $message =~ s/\n/\<br\>/g; Output: This is a test message. This is line 2 of test message. If I View Source it shows: This is a test message. This is line 2 of test message.
      I'm wondering if I have to do something special before it gets stuffed in the database, but I don't think so. I'm doing it the same way I always have.

        I'm afraid that won't work

        Use Data::Dumper or Data::Dump to Dump data from your program as perl structures, and then show us that

        I'm wondering if I have to do something special before it gets stuffed in the database, but I don't think so.

        As a general rule, you do not want to do something special before the database. You want to do something special before the email. When you need a particular encoding or alteration of your data for one use, you'll find yourself having to backtrack less often by doing that transformation as late and as local as possible.

Re: Converting carriage returns to HTML breaks
by thomas895 (Deacon) on Apr 16, 2012 at 03:48 UTC

    You can also cheat:

    use CGI; my $html_break = CGI->br; $message =~ s/\Q\r\n\E/$html_break/g;

    Not sure if that does what you want, but it's worth a shot.

    ~Thomas~
Re: Converting carriage returns to HTML breaks
by Anonymous Monk on Apr 16, 2012 at 03:23 UTC
    split() the string using a regex that looks for [\n\r]{1,2} then join() the string using <br/> as the separator.

      I think that should be  [\n\r]{1,2} and  '<br/>' and whether it will actually work is another question.

Re: Converting carriage returns to HTML breaks
by mendeepak (Scribe) on Apr 16, 2012 at 04:48 UTC

    i will back chromatic... try \r for \n other wise just put the sample input data and output data

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-29 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found