<pre> and wordwrap

C

Christoph

I have a large amount of text that is getting pulled from a file. It looks
something like this:

'large amount of text 90+ characters long

large amount of text 90+ characters long

large amount of text 90+ characters long'

If I don't enclose the text in a <pre> tag, all the CRLFs are stripped and
all the text runs together. If I do put it in a <pre> tag, the text goes
off the right side of the screen and causes a scroll bar to appear on the
browser (due to where the text is positioned, by necessity, on the page).
So is there something I can do, using CSS or anything else, that gives me
the best of both worlds? That will (effectively) keep the CRLFs and also
wrap the text?

thnx,
Christoph
 
T

Toby Inkster

Christoph said:
If I don't enclose the text in a <pre> tag, all the CRLFs are stripped and
all the text runs together. If I do put it in a <pre> tag, the text goes
off the right side of the screen and causes a scroll bar to appear

The CSS answer is "white-space:pre-wrap" which is in the CSS 2.1 draft,
but it's not widely supported by browsers.

The alternative is to use server-side scripting to format the text
appropriately. There are two ways to do this:

1. Use PHP's wordwrap() function (or an equiv function in another
language) to ensure that no lines are too long, before dumping it
all into a <pre>...</pre> block; or

2. Use PHP's nl2br() function (or equiv) to convert the existing
line breaks into <br> tags, then dump the whole thing into a
<div>...</div> block.

The second solution is probably a better idea, because it allows the
browser to do its own word wrapping. The browser will almost certainly do
a better job at wrapping than the server would have.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top