Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<pre></pre> doesn't consider wordwrap spaces

Status
Not open for further replies.

breaststroke

Programmer
Apr 10, 2011
39
ES
Hello,

I have an everlasting problem with my textareas.:(

I have a textarea input which inserts a text in my database.
This is okay.
The problem/s comes when I take that text out of the database and print it on the screen.
If I use the nl2br() function, I manage to "print" or respect the newlines of my text.
If I use wordwrap function, I manage to break long words into smaller ones.
Then there wouldn't be any problem.
But these functions are not enough to "respect" or consider any space (/r/s); for example, longer-than usual spaces between words, text-indent at the beginning of the sentence, etc..
So, I have tried using :<pre>$text</pre>;and it works and respects those spaces.

Then, the idea is to apply these three functions (or just the last two).
The problem comes when there are very long words. Now wordwrap does its job again and divide the long words into smaller ones, but there is no "newline" when it should be in order for the text to fit into the div dimensions, even though the div has "width" already set.
So, for example, if I had a 50px wide div, the result applying <pre></pre> could be:
"asd alsdj asñdlk asñdkasdñapwoe asñdlka dñkasd asñkd ásñld...",

so it would respect the wordwrap function, but it wouldn't shorten the long lines. (something that did happen without <pre></pre>).

I hope I have managed to explain my problem more or less. Sorry for my English.

I would appreciate any help.Thank you very much in advance.
Regards
 
hello again,

to explain it a bit better:

<pre></pre> doesn't seem to consider the spaces generated by wordwrap as spaces, at least when it comes to make mew lines.

so, how could I print my texts respecting all kind of spaces?

I would really really appreciate any help.

p.s another problem related to this is the fact that quite frequently, from a certain line, lines are printed as bold ones.


Regards again,

ps:and sorry , I had to post this reply because I couldn't edit the previous one.
 
Hi

The whitespace characters inside [tt]pre[/tt]tags should be preserved, including the newline characters. But HTML tags are also interpreted. So applying both [tt]nl2br()[/tt] and [tt]pre[/tt] to the text should result in double spaced lines. ( Empty lines between the lines of text. )

But that is only the default behavior, CSS can alter it. Maybe Jeff Starr's article Wrapping Long URLs and Text Content with CSS will give some hints.
breaststroke said:
p.s another problem related to this is the fact that quite frequently, from a certain line, lines are printed as bold ones.
That sounds like your text contains HTML markup ( or something similar, even if invalid ), so use [tt]htmlentities()[/tt] or [tt]htmlspecialchars()[/tt].

Better post some real examples, both for the text and your PHP code.

Feherke.
 
Hello,

thank you very much feherke.

I checked out that link and it was really helpful.
The script explained about in it wasn't good for my pages but I did some changes and, at least so far, is working fine.
I added some attributes to the "pre" tag, such as:
.wrapped{white-space:pre-wrap;word-wrap:break-word;width:XXpx;}

And the problem with darker lines disappeared with it as well.
In any case I will look into it to avoid future problems.

And I was wrong (as I think you suspected); the problem didn't have anything to do with wordwrap function. Pre tag wasn't preserving whitespaces between words regardless if I had applied the wordwrap function before or not.


Thank you so much again. If I can help you on anything just tell me please.[dazed]

REgards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top