PHP nl2br() 基本函数

标签 php line-breaks

有人可以帮我处理这个邮件脚本吗?

我只是想发送一封 html 电子邮件,部分消息来自用户文本区域,其中输入\r\n。

我似乎无法使用 nl2br 或任何其他类似的功能。下面的代码不是我正在使用的,但仍然会产生错误。

代码:

$to  = 'example@gmail.com';

$subject = 'Test Subject';

$message_var_1 = 'test1 \r\n test2 \r\n test3';
$message = nl2br("
    <div>
    <div>$message_var_1</div>
    </div>
");

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion() . "\r\n";

mail($to, $subject, $message, $headers);

最佳答案

$message_var_1 = 'test1 \r\n test2 \r\n test3';

PHP 仅在 " 内解析 \r\n,而不是在 ' 内。因此 nl2br 在这里不适用。

$message_var_1 = "test1 \r\n test2 \r\n test3";
$message = '<div>
     <div>'.nl2br($message_var_1).'</div>
</div>';

这应该有效。

关于PHP nl2br() 基本函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32634102/

相关文章:

php - Laravel/PHP str_replace 函数中的单引号显示笑脸图像

html - CSS 中的换行 <tr> 内容

emacs orgmode 不在标题之间插入行?

css - 控制换行符使线条宽度相等

php - 如何将图片路径和名称上传到数据库 - Codeigniter

php - Azure PHP MySQL 应用内 - 无法从 PHP 连接到 MySQL

php - 安全地获取已定义和未定义索引的数组元素值

javascript - 什么时候可以在字符串中换行?

php 替换多个 br 标签

PHP LDAP 连接