Preformatted text with wrap

J

Joseph Misko

I am using MySQL to serve plain text email newsletters using PHP. I have
the text of the emails stored, and I thought the <pre> tag would be my
answer. However, that doesn't break at the page width. Basically, I need a
tag or option that allows me to keep the paragraph and linebreak formatting,
but will wrap and not produce lines a thousand characters long.

Thanks,
Joe
 
D

Dan

Joseph said:
I am using MySQL to serve plain text email newsletters using PHP. I have
the text of the emails stored, and I thought the <pre> tag would be my
answer. However, that doesn't break at the page width. Basically, I need a
tag or option that allows me to keep the paragraph and linebreak formatting,
but will wrap and not produce lines a thousand characters long.

If the plain text email was properly formatted to begin with, there
wouldn't be "lines a thousand characters long"; RFC 2822 imposes an
absolute maximum line length of 998 characters, but strongly suggests
keeping lines to 78 characters at the most unless there's a really good
reason to do otherwise. If it conforms to this, it should display well
in most normal screen resolutions within <PRE> elements.

http://mailformat.dan.info/body/linelength.html
 
B

Beauregard T. Shagnasty

Dan said:
If the plain text email was properly formatted to begin with, there
wouldn't be "lines a thousand characters long"; RFC 2822 imposes
an absolute maximum line length of 998 characters, but strongly
suggests keeping lines to 78 characters at the most unless there's
a really good reason to do otherwise. If it conforms to this, it
should display well in most normal screen resolutions within <PRE>
elements.

If the email is to be plain text, there should be no HTML elements at

My sites all have contact forms that gather plain text - in long lines
until Return is pressed, send it via PHP as a plain text email, and
the lines wrap normally, according to the receiving email client.
 
A

Adrienne

I am using MySQL to serve plain text email newsletters using PHP. I
have the text of the emails stored, and I thought the <pre> tag would
be my answer. However, that doesn't break at the page width.
Basically, I need a tag or option that allows me to keep the paragraph
and linebreak formatting, but will wrap and not produce lines a
thousand characters long.

Thanks,
Joe

http://www.php.net/chr might be able to help.
 
T

Toby Inkster

Joseph said:
I am using MySQL to serve plain text email newsletters using PHP. I have
the text of the emails stored, and I thought the <pre> tag would be my
answer. However, that doesn't break at the page width. Basically, I need a
tag or option that allows me to keep the paragraph and linebreak formatting,
but will wrap and not produce lines a thousand characters long.

There are two solutions -- client-side and server-side. Neither are ideal
though and a better solution would be to avoid the use of <pre> for this
purpose.

Client-side - use the "white-space:pre-wrap" property of CSS 2.1:

<pre style="white-space:pre-wrap">
This is some preformatted text, but very long lines will be wrapped
automatically.
</pre>

Drawback of "pre-wrap" is that several browsers, including Internet
Explorer, don't support it. D'oh!

Server-side - use PHP's "wordwrap()" function:

<pre><?php print wordwrap($mytext, 72) ?></pre>

Drawback here is it will wordwrap your text at (in the example above) 72
characters, no matter how wide or narrow the visitor's screen is. D'oh
again! Also, it's not supported in PHP before 4.0.2.

A better idea might be to avoid using <pre> altogether. For example:

<?php
$mytext = function_to_get_it_from_database();
$mytext = htmlspecialchars($mytext);
$mytext = str_replace("\r\n", "\n", $mytext);
$mytext = str_replace("\n\n", '</p><p>', $mytext);
$mytext = str_replace("\n", '<br>', $mytext);
print "<p>${mytext}</p>";
?>

might do the job for you.
 
J

Joseph Misko

Beauregard T. Shagnasty said:
If the email is to be plain text, there should be no HTML elements at


My sites all have contact forms that gather plain text - in long lines
until Return is pressed, send it via PHP as a plain text email, and the
lines wrap normally, according to the receiving email client.

I'm not asking about sending email. I am asking about how to publish a
plain text file with long lines in HTML on the web. I need the text to wrap
and yet I do not want to put all of the text through a processor to wrap
each separate paragraph in <p> tags, but I basically want the same effect.

Thanks,
Joe
 
J

Joseph Misko

A great idea, the last one. All three actually, and the only workable
suggestions I have received from a few different groups. I will give the
str_replace a whirl.

Thanks,
Joe
 
B

Beauregard T. Shagnasty

Joseph said:
I'm not asking about sending email. ...

Yes you were. Nowhere in your original post did you say you wanted to
display your plain text email newsletters as HTML pages.

If that is really your question, then putting your long lines
(paragraphs, I suppose) within <p>...</p> would wrap the text within
the bounds of the visitor's browser and the container they were shown
in. One step towards a flexible design, too.
 
J

Joseph Misko

Beauregard T. Shagnasty said:
Yes you were. Nowhere in your original post did you say you wanted to
display your plain text email newsletters as HTML pages.

Sorry about the poor wording. I want to display a huge number of plain text
email archives online using HTML. I want to avoid need to reformat
(including add said:
If that is really your question, then putting your long lines (paragraphs,
I suppose) within <p>...</p> would wrap the text within the bounds of the
visitor's browser and the container they were shown in. One step towards a
flexible design, too.

My ideal is to work with the text as it is. Seems there must be some way to
(1) work with the existing formatting, (2) just wrap whenever we hit screen
width. Makes sense to me. Not possible with HTML?

Thanks,
Joe
 
J

Joseph Misko

str_replace seems to have worked a charm! Before your post, working with
the text like this had crossed my mind, but I thought it would be too
laborious. It's simple.

Many thanks,
Joe
 
D

Dan

Beauregard said:
If the email is to be plain text, there should be no HTML elements at
all, including <pre>, as I see it...

The original poster didn't say that there were any tags in the original
email, just that he was putting the email contents on a Web page
between said:
My sites all have contact forms that gather plain text - in long lines
until Return is pressed, send it via PHP as a plain text email, and
the lines wrap normally, according to the receiving email client.

If you want to comply properly with the relevant standards (RFC 2822),
you need to run the text through some server-side routine that
reformats them to proper line length with hard breaks, before sending
it as email.
 
B

Beauregard T. Shagnasty

Dan said:
The original poster didn't say that there were any tags in the
original email,

That is correct.
just that he was putting the email contents on a Web page between
<pre>...</pre> tags.

No, he never mentioned "Web page" implying that he was attempting to
format an email message. His recent reply noted the poor wording of
his original question. <g>
 
T

Toby Inkster

Joseph said:
str_replace seems to have worked a charm! Before your post, working with
the text like this had crossed my mind, but I thought it would be too
laborious. It's simple.

No problem. PHP has some really nice string manipulation functions.
Arguably more powerful than Perl[1] although the syntax is not quite
as easy to use.

____
1. Justification: in PHP, for most string operations (matching, replacing,
splitting, etc) you can choose between Perl-compatible regular expressions,
extended regular expressions, simple strings and so forth. With Perl you
pretty much always have to use regular expressions.

,=======================================================================.
| | matching | replacing | splitting |
|-----------------------------------------------------------------------|
|Simple strings | strstr() | str_replace() | explode() |
|Ditto Case-Ins | stristr() | str_ireplace() | NO SUCH FUNCTION|
|Extended Reg Exp | ereg() | ereg_replace() | split() |
|Ditto Case-Ins | eregi() | eregi_replace() | spliti() |
|Perl-compat Reg Exp | preg_match() | preg_replace() | preg_split() |
`======================================================================='
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top