Preformatted text with wrap

Discussion in 'HTML' started by Joseph Misko, Jun 21, 2005.

  1. Joseph Misko

    Joseph Misko Guest

    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
     
    Joseph Misko, Jun 21, 2005
    #1
    1. Advertising

  2. Joseph Misko

    Dan Guest

    Joseph Misko wrote:
    > 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

    --
    Dan
     
    Dan, Jun 22, 2005
    #2
    1. Advertising

  3. Dan wrote:
    > Joseph Misko wrote:
    >
    >> 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.


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

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


    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.

    --
    -bts
    -This space intentionally left blank.
     
    Beauregard T. Shagnasty, Jun 22, 2005
    #3
  4. Joseph Misko

    Adrienne Guest

    Gazing into my crystal ball I observed "Joseph Misko" <>
    writing in news::

    > 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.

    --
    Adrienne Boswell
    http://www.cavalcade-of-coding.info
    Please respond to the group so others can share
     
    Adrienne, Jun 22, 2005
    #4
  5. Joseph Misko

    Toby Inkster Guest

    Joseph Misko wrote:

    > 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.

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
     
    Toby Inkster, Jun 22, 2005
    #5
  6. Joseph Misko

    Joseph Misko Guest

    "Beauregard T. Shagnasty" <> wrote in message
    news:tD4ue.31517$...
    > Dan wrote:
    >> Joseph Misko wrote:
    >>
    >>> 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.

    >
    > If the email is to be plain text, there should be no HTML elements at
    > all, including <pre>, as I see it...
    >
    >> http://mailformat.dan.info/body/linelength.html

    >
    > 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
     
    Joseph Misko, Jun 22, 2005
    #6
  7. Joseph Misko

    Joseph Misko Guest

    "Adrienne" <> wrote in message
    news:Xns967CD9149497arbpenyahoocom@207.115.63.158...
    >
    > http://www.php.net/chr might be able to help.
    >


    Which usage were you thinking?

    Joe
     
    Joseph Misko, Jun 22, 2005
    #7
  8. Joseph Misko

    Joseph Misko Guest

    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

    "Toby Inkster" <> wrote in message
    news:p...
    > Joseph Misko wrote:
    >
    >> 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.
    >
    > --
    > Toby A Inkster BSc (Hons) ARCS
    > Contact Me ~ http://tobyinkster.co.uk/contact
    >
     
    Joseph Misko, Jun 22, 2005
    #8
  9. Joseph Misko wrote:
    > "Beauregard T. Shagnasty" <> wrote
    >
    >>> Joseph Misko wrote:
    >>>
    >>>> I am using MySQL to serve plain text email newsletters using
    >>>> PHP. ...


    >>> If the plain text email ...

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

    >
    > 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.

    --
    -bts
    -This space intentionally left blank.
     
    Beauregard T. Shagnasty, Jun 22, 2005
    #9
  10. Joseph Misko

    Joseph Misko Guest

    "Beauregard T. Shagnasty" <> wrote in message
    news:XKdue.48976$...
    >
    > 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 <p> tags) to the text if possible.

    > 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
     
    Joseph Misko, Jun 22, 2005
    #10
  11. Joseph Misko

    Joseph Misko Guest

    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
     
    Joseph Misko, Jun 22, 2005
    #11
  12. Joseph Misko

    Dan Guest

    Beauregard T. Shagnasty wrote:
    > 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 <pre>...</pre> tags.

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

    >
    > 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.

    --
    Dan
     
    Dan, Jun 22, 2005
    #12
  13. Dan wrote:
    > Beauregard T. Shagnasty wrote:
    >
    >> 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,


    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>

    --
    -bts
    -This space intentionally left blank.
     
    Beauregard T. Shagnasty, Jun 22, 2005
    #13
  14. Joseph Misko

    Toby Inkster Guest

    Joseph Misko wrote:

    > 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() |
    `======================================================================='

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
     
    Toby Inkster, Jun 22, 2005
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sara T.

    WRAP text in <TD>

    Sara T., Jul 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    654
    Sara T.
    Jul 26, 2003
  2. Dan
    Replies:
    2
    Views:
    2,114
    Tommy
    Mar 2, 2004
  3. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    737
    Chronic Philharmonic
    May 10, 2008
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    270
    Art Werschulz
    Sep 22, 2003
  5. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    277
    Anno Siegel
    Sep 25, 2003
Loading...

Share This Page