PDA

View Full Version : my own wrap subroutine


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/ /&nbsp;/g; $texT=~ s/\n/<br>/g;$texT =~ s/<$1>//g;
print $texT;

}

mlseim
11-17-2005, 04:42 PM
I think you're making this harder than it needs to be ...

Why not use a CSS style sheet (or style insert) to format that space
so it will automatically fit the text ... either justified or non-justified?

The Perl script would write the text without any line breaks or new-lines.

The CSS style sheet would properly display the contents on the page.

In fact, you can mix your tables with CSS or eliminate the tables and
go with pure CSS style sheets (I personally suggest the latter).


EDIT:
I see you're already using some style sheet properties ....

<div style="margin: 5pt 4pt">

<!--ARTIKEL-START-->


Maybe you can add more to it like this ...

<div style="margin: 5pt 4pt; padding: 0px; width: 360px; text-align: justified;">

<!--ARTIKEL-START-->

skipion
11-17-2005, 07:02 PM
thanks for replying.. well main issue is that the words don't get wrapped when using a div. I can use a div like this: <div id="topmenu" style="word-wrap: break-word; width: 600px; padding:12px;> with the word-wrap style in it. That works fine in IE, but it doesnt work at all at firefox.

mlseim
11-17-2005, 08:34 PM
Firefox doesn't like the tables <tr><td> ...

I think it puts those ahead (default) of any <div>'s.

You really need to go with no tables at all.

Use a separate .css style sheet so that you can link to and use it
with all your separate pages. That way, each page follows the
same .css style sheet.

Doing that, it would render correctly in all browsers except
Macintosh IE (which is always broken).

If you really need help with that, let me know ... I could give you
a great example using your existing site (in a couple of days time).

skipion
11-17-2005, 09:48 PM
Well actually i didnt use any table at all.. The div increases in size when a very long word is within the div (only in firefox). A lot of people seem to have the same problem as me, namely the word-wrap within firefox, and there doesnt seem to be any solution to it.

So the only option is to do the wrapping server-side. Any help on my script would be appreciated.

mlseim, thanks for your help so far.

mlseim
11-18-2005, 03:51 AM
I just threw this example together in about 15 minutes,
to give you an idea of what I'm talking about.

Note.... no tables at all, and the text is all one line (no line breaks),
yet it looks just fine using CSS.

Here's my simplistic example:
http://www.catpin.com/nu/

Here's the style sheet:
http://www.catpin.com/nu/main.css

With some more <div id's> and some more accurate tweaking
and positioning, your site would render beautifully in all browsers
without any server-side wrap stuff.

I just think you're making it really difficult for yourself, and could
take advantage of using CSS as it's designed.

Like I say ... I spent a whole 15 minutes on this, and it doesn't
look too bad to me.

.... even though I don't understand a word of your language..lol

EDIT AGAIN ...

and with words like this: "uitvoeringsverslagen" (what the?)
... 20 letters -- no wonder why you're having trouble wrapping.


.

Jeff Mott
11-18-2005, 05:52 AM
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 themSince you're here and asking for help, why not try to get this figured out instead?...with the word-wrap style in itIn the world beyond Microsoft, word-wrap is not an actual style property. See the CSS specification's property index (http://www.w3.org/TR/CSS21/propidx.html) for a full list of standard/official properties.