4 replies [Last post]
rsiedl
Offline
newbie
Perth Australia
Last seen: 11 years 19 weeks ago
Perth Australia
Joined: 2003-06-12
Posts: 3
Points: 0

Hi,

I'm trying to get some text that I have in my MySql database onto a html page.

In order to keep the formatting \n etc, I'm using the pre tags but have run into a problem when the text runs over the width of the page.

Is it possible to get pre to wrap text at a certain width?

Cheers,

Reagen.

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 15 hours 11 min ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5125
Points: 2722

pre wrap

Hi rsiedl,
Have you tried setting the width and overflow
pre {width:300px; overflow:scroll;}

Otherwise you could count characters as the come from the database and find the closest space to the width you want then force a break.
Uhmm scrollbars are starting to look really good. Wink

rsiedl
Offline
newbie
Perth Australia
Last seen: 11 years 19 weeks ago
Perth Australia
Joined: 2003-06-12
Posts: 3
Points: 0

RE: pre wrap

Hey Tony,

Thanks for the suggestion and yeah I had thought of that. Unfortunately the page needs to be printed so an overflow is not really an option.

Had also thought about creating a "Perl wrap script" but was really really hoping there would be an easier way to get around this. I should know by now that nothing is ever easy.

Cheers.

dJomp
dJomp's picture
Offline
Enthusiast
York, England
Last seen: 11 years 31 weeks ago
York, England
Joined: 2003-03-23
Posts: 422
Points: 0

Can pre tags handle word wrap?

Wandering from CSS to SQL...

When you get the data from the database, could you not run a command on it to convert all \n's to <br>'s? Something like:
SELECT id, REPLACE(blurb,"\n","<br>"), ... FROM x ...
Not exactly sure how you'd represent the \n mind, I suppose just a line break in the SQL query.

Hmm, that could make my life a lot easier in one of my scripts... why did I never think of that before?

You know you're a geek when you try to shoo a fly away from the monitor with your cursor.

rsiedl
Offline
newbie
Perth Australia
Last seen: 11 years 19 weeks ago
Perth Australia
Joined: 2003-06-12
Posts: 3
Points: 0

/n tags replaced

that though also did cross my mind but I ruled it out because of things like whitespaces. You would have to replace all those as well so that the user gets the same text as what they inputed. And im not sure what else pre prints that I would have to check for and replace.

In the end I have just used a perl sub to "wrap" the text.

##############################################
# Subroutine: wrapText ( line_to_wrap_at, text_to_wrap )
# Takes some text and wraps it to the number of characters specified.
# Returns the wrapped text.
##############################################
sub wrapText {
        my ($wrap,$buffer) = @_;
        my ($start, $rest, $tmp, $something);
        ### Need to isolate words longer than the wrap size ...
        $buffer =~ s/([^\s]{$wrap,})\s/\n$1\n/g;
        ### ... and then do real wrapping.
        while ($buffer =~ /([^\n]{$wrap})/) {
                $start = $`;
                $rest = $';
                $something = $1;
                $something =~ s/((.|\n)*)\s((.|\n)*)/$1\n$3/;
                $something =~ /((.|\n)*)(\n.*)/;
                $tmp .= $start . $1;
                $buffer = $3 . $rest;
        }
        $buffer = $tmp . $buffer;
        return $buffer;
}