At 11:42 07.06.2001 -0400, you wrote:
>I have a problem with an HTML form. I am collecting in put from a html
>textarea box, then displaying it to the scream on the next page. the
>problem is that the whole string is being displayed all on one line I want
>no more than 50 characters to display on each line. And only if the string
>is more than 50 characters. Does anyone have a solution?
Text in a <textarea> does not include line breaks unless the user inserts
them as they type. Your best bet would be to write a sub that parses the
text and inserts <br>'s at the appropriate places. Something like:
my $output = "";
while($text)
{
$output .= substr($text, 0, 50) . "<br>";
$text = substr($text, 50);
}
print "$output";
however, this just sticks <br>'s in willy-nilly every 50 characters, which
is kind of ugly, so you'll want to check to make sure that you're breaking
the line at the end of a word.
like this:
my $output = "";
my $count = 0;
for(my $i = 0; $i < length($text); $i++)
{
if($count < 50)
{
$output .= substr($text, $i, 1); # add one character to
output at a time until we're up to fifty chars
$count++;
}
else
{
my $next_char = substr($text, $i, 1);
while($next_char ne " ")
{
$output .= $next_char;
$i++;
$next_char = substr($text, $i, 1);
}
$output .= "<br>";
$count = 0;
}
}
If it absolutely *has* to be fifty characters per line, then you should add
hyphens at the end of lines that break in the middle of a word.
my $output = "";
my $count = 0;
for(my $i = 0; $i < length($text); $i++)
{
$output .= substr($text, $i, 1); # add one character to output at
a time until we're up to fifty chars
if($count == 49)
{
my $next_char = substr($text, $i, 1);
$output .= ($next_char =~ /\s$/ || $next_char eq "")?
"<br>" : "-<br>"; # check to see if the next character is a space or an
empty string, if it isn't, add the dash
$count = 0;
}
else
{
$count++;
}
}
This code could be written in fewer lines, but it's nice and clear this
way, so I'll leave it :)
Aaron Craig
Programming
iSoftitler.com