skipion
11-17-2005, 12:07 PM
I'm trying to write my own wrap subroutine, since word-wrapping in html seems to work in IE, but not in Firefox. So it has to be done server side. I've looked both into the text::wrap and text::format modules, but to be honoust I don't have a clue how to invoke them, i've tried a couple of things but i never got the result i was looking for. So I decided to write a subroutine myself. That way I know what's happening and I can allways adjust it later on. So anyway I've included my script below, and it needs to be called like this:
$texT=$q->param('mytext');
$regelmax="72"; #max number of chars on one line
&mywrap($texT,$regelmax);
So why am I writing here ? Cause there are 2 issues there need to be fixed to get it working 100%. I appreciate if someone is willing to take a look at it. I copied/pasted the text from:
http://www.nu.nl/news/627204/10/%27Wachtlijsten_in_de_zorg_zijn_nooit_weggeweest%27.html
(from the words: 'DEN HAAG' till 'niet meer nodig' at the end)
and pasted that into my form's textarea, I then submitted the thing and the cgi script invokes my subroutine with the 3 lines above.
So the errors I discovered are:
the script places some <br>'s at the wrong places, in the copy-paste sample it generetes two <br>'s where there should be one, it also creates an additional <br> after the word 'bovendien'.
if someone enters a lot of characters like 80, where i got a max number of 72 chars, the line has to be broken at the 72 character and the rest of the characters need to be placed on the next line.. however I dont know how to invoke this within my script.
sub mywrap{
my ($texT,$regelmax)=@_;
for ($teller=0;$teller<length($texT);$teller=$teller+$regelmax){
if (substr($texT,$teller,1) ne " "){
$positie=rindex($texT," ",$teller);
if ($positie>0){substr($texT,$positie,1)="<br>"}
}else{
substr($texT,$teller,1)="<br>";
}}
$texT=~ s/ / /g; $texT=~ s/\n/<br>/g;$texT =~ s/<$1>//g;
print $texT;
}
$texT=$q->param('mytext');
$regelmax="72"; #max number of chars on one line
&mywrap($texT,$regelmax);
So why am I writing here ? Cause there are 2 issues there need to be fixed to get it working 100%. I appreciate if someone is willing to take a look at it. I copied/pasted the text from:
http://www.nu.nl/news/627204/10/%27Wachtlijsten_in_de_zorg_zijn_nooit_weggeweest%27.html
(from the words: 'DEN HAAG' till 'niet meer nodig' at the end)
and pasted that into my form's textarea, I then submitted the thing and the cgi script invokes my subroutine with the 3 lines above.
So the errors I discovered are:
the script places some <br>'s at the wrong places, in the copy-paste sample it generetes two <br>'s where there should be one, it also creates an additional <br> after the word 'bovendien'.
if someone enters a lot of characters like 80, where i got a max number of 72 chars, the line has to be broken at the 72 character and the rest of the characters need to be placed on the next line.. however I dont know how to invoke this within my script.
sub mywrap{
my ($texT,$regelmax)=@_;
for ($teller=0;$teller<length($texT);$teller=$teller+$regelmax){
if (substr($texT,$teller,1) ne " "){
$positie=rindex($texT," ",$teller);
if ($positie>0){substr($texT,$positie,1)="<br>"}
}else{
substr($texT,$teller,1)="<br>";
}}
$texT=~ s/ / /g; $texT=~ s/\n/<br>/g;$texT =~ s/<$1>//g;
print $texT;
}