开发者

PHP's nl2br not working (nor alternatives)

I'm working on a contact form. The contact message can have line breaks. They need converted to <br>. Nothing is working. Here's my test code:

mail( $email_to, $email_subject,

      '<html><head><title>' . $email_subject . '</title></head><body>'
    . $text_before_message
    . str_replace( array("\\r\\n","\\r","\\n"), "<br />", stripslashes($email_body) )
    . str_replace( array("\r\n","\r","\n"), "<br />", stripslashes($email_body) )
    . nl2br( stripslashes($email_body) )
    . str_replace( array('\\r\\n','\\r','\\n'), "<br />", stripslashes($email_body) )
    . str_replace( array('\r\n','\r','\n'), "<br />", stripslashes($email_body) )
    . stripslashes( nl2br($email_body) )
    . nl2br($email_body)
    . $text_after_message
    . '</body></html>'

    , 'MIME-Version: 1.0' . "\r\n"
    . 'Content-type: text/html; charset=UTF-8' . "\r\n"
    . 'From: ' . $email_from . "\r\n"
    . 'To: ' . $email_to . "\r\n"    
);

When entering:

Test.

Test.

The result in the email is (repeated several times because of the tests):

Test.Test.

Which is spaced with regular line breaks (not <br>) if I look at the source code.

Why is 开发者_开发知识库PHP doing this to me? This question appears to be asked a lot, but the solutions I can find... make no difference what-so-ever.

How can I convert the line breaks to html breaks?


Quick answer: use nl2br().

In other words, it works for me. I prefixed your code with:

$email_to="...me...";
$email_from=$email_to;
$email_subject="HTML test";

$text_before_message='<div style="color:red">BEFORE</div>';
$text_after_message='<div style="color:red">AFTER</div>';
$email_body=<<<EOD
1
2

3


4 (after two blank lines)
EOD;

In Thunderbird, at least, I see a text email with correct line spacing (for the nl2br() ones). When I view as HTML I also see correct spacing, and the red text at top and bottom.

It is possible you are using an email client that doesn't understand <br />, and wants to see <br>?! Specifying an explicit doctype might help (but a quick google suggests it won't).

Debug tip: when everything that should work does not, check doing it manually works. I.e. include a test using a block of static HTML that uses <br /> (and <br>) and see if it works. If it doesn't you know the "which php function do I use" is the wrong question.


Using the double backslash in str_replace function worked for me.

http://php.net/manual/en/language.types.string.php To specify a literal single quote, escape it with a backslash (). To specify a literal backslash, double it (\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.


Why make it so hard when it can be so easy?

//here is the pull from the form
$your_form_text = $_POST['your_form_text'];

//line 1 fixes the line breaks - line 2 the slashes
$your_form_text = nl2br($your_form_text);
$your_form_text = stripslashes($your_form_text);

//email away
$message = "Comments: $your_form_text";
mail("destination_email@whatever.com", "Website Form Submission", $message, $headers);

You will obviously need headers and likely have more fields, but this is your textarea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜