Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you keep carriage returns in a string? 1

Status
Not open for further replies.

CGIflustered

Technical User
Feb 12, 2002
11
US
Hi,

I have a form which submits some variables for processing to a HTML file. Here's how the script handles them:

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$FORM{$name} = $value;
}


The only problem is that one variable ("comment", a TEXTAREA input) has to have the carriage returns included for what I need. When I print out "comment" now, all lines are put together like one big sentence. How do I omit this one variable from stripping the carriage returns?

Thanks in advance,
Mark
 
Code:
<textarea name=&quot;some_name&quot; rows=&quot;5&quot; cols=&quot;40&quot;
[red]
Code:
wrap=&quot;hard&quot;
[/red]
Code:
>
Some text here.</textarea>

Try setting 'wrap' to hard in your HTML input field tags. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
goBoating,

the textarea entry input on the form itself works fine... test already wraps, and user can carriage return, but when I process it with the script above, it takes out the carriage returns and makes, text like this (all one string):

Hello.

My name is Mark.

How are you?


Look like this:

Hello. My name is Mark. How are you?

How do I get the string to remain as it was input?

Thanks again,
Mark
 
Some HTML containing a single textarea input field with wrap='hard'.
Code:
<html>
<head><title>junk</title></head>
<body>
<form action=&quot;[URL unfurl="true"]http://server/cgi-bin/junk.cgi&quot;>[/URL]
<textarea name=&quot;area51&quot; rows='4' cols='25' wrap='hard'>
Some text here
</textarea>
<input type='submit' name='submit' value='submit'>
</form></body></html>

A piece of CGI to catch the area51 textarea input and
spit it back at the browser.
Code:
#!/usr/local/bin/perl
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
	$buffer = $ENV{'QUERY_STRING'}; 
	}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
	read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
	}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
   ($name, $value) = split(/=/, $pair);

   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
   $name =~ tr/+/ /;
   $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;, hex($1))/eg;
   $FORM{$name} = $value;
}

# Note that the stuff above is just like yours, no worrying
# about carriage returns.  They are still embedded in the
# input.
print qq(Content-type: text/html\n\n
    <html>
      <head>
        <title></title>
      </head>
      <body><p>);

# grab the area51 input text
$textarea = $FORM{'area51'};

# There are '\n's in there but, the browser does not
# obey \n, so I enclose the text in 'pre' tags.
print &quot;<pre>$textarea</pre>&quot;;

print &quot;<b>OR</b><br><br>&quot;;

# an alternate method is to put 'br' tags in with
# the \n's so the browswer knows to wrap.
$textarea =~ s/\n/<br>\n/gs;
print &quot;$textarea&quot;;
print qq(</p></body></html>);


'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
That's it! It worked!!

Could you break down this line for me?:

$textarea =~ s/\n/<br>\n/gs;

I understand it's putting <BR> into the generated HTML... what's the other stuff?

Thanks for all your help,
Mark

 
the general form for a Perl regular expression
replacement is,

$var =~ s/find_this/replace_with_this/;

If $var contained a string, you could replace some
portion of the string.

$var = 'a series of words to play with words';
$var =~ s/words/terms/;

That would replace the first instance of 'word'
with 'term'. But, we need to replace all instances
of 'word', so we add the 'g' switch to make the
regex [red]g[/red]obal.

$var = 'a series of words to play with words';
$var =~ s/words/terms/[red]g[/red];

The Perl regex engine stops at line endings. If
we want our replace to work across multiple lines
add the 's' switch.

$var = &quot;some words in a string to \n play with
so we can see which words turn into terms&quot;;

$var =~ s/words/term/gs;

So,
Code:
$textarea =~ s/\n/<br>\n/gs;
                | \    / ||
                |  \  /  | treat multiple lines as 1 string
                |   \/   run globally
                |   the replacement string is a '<br>'
                |   followed by a new line
               find new lines










'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
>>>hope this helps

It absolutely does. Thanks for all the help!

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top