html textarea --> php mail function produces additional line break

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Schinzel

    html textarea --> php mail function produces additional line break

    Hi,

    I'm reading text from a html textarea field (standard wrap
    functionality, no value specified). If I display it via nl2br() (after
    html form submission), everything's ok. But if I send it via mail() the
    receiver gets each single line break (originating from textarea)
    displayed by TWO line breaks. So the text needlessly gets inflated...
    Can anybody help me out what to do to handle this problem?

    Thanks in advance!

    Regards,
    Christian Schinzel

    (copied from php.general)

  • Pedro Graca

    #2
    Re: html textarea --> php mail function produces additional line break

    Chris Schinzel wrote:[color=blue]
    > I'm reading text from a html textarea field (standard wrap
    > functionality, no value specified). If I display it via nl2br() (after
    > html form submission), everything's ok. But if I send it via mail() the
    > receiver gets each single line break (originating from textarea)
    > displayed by TWO line breaks. So the text needlessly gets inflated...
    > Can anybody help me out what to do to handle this problem?[/color]

    Different clients will send line breaks differently:

    un*x -- \n
    mac -- \r
    win -- \r\n
    ??? -- ???

    So normalize input before anyhting else.

    <?php
    function normalize_lineb reaks($text) {
    $text = str_replace("\r \n", "\n", $text); /* win -> un*x */
    $text = str_replace("\r ", "\n", $text); /* mac -> un*x */
    return $text;
    }

    mail($recipient , $subject, normalize_lineb reaks($_POST['text_area']));
    ?>
    --
    Mail to my "From:" address is readable by all at http://www.dodgeit.com/
    == ** ## !! ------------------------------------------------ !! ## ** ==
    TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
    may bypass my spam filter. If it does, I may reply from another address!

    Comment

    • Chris Schinzel

      #3
      Re: html textarea --&gt; php mail function produces additional line break

      Great - that's it! Thank you very much! :-)

      Comment

      Working...