替换电子邮件回车(Replace carriage return in an email)

2019-09-30 04:56发布

我想在PHP换行符替换回车,这样我的网站版主不必键入
每次他们想打字从我的站点中的邮件时添加一个新行。 我尝试过几种不同的方法来代替换行符但都没有奏效。 我曾尝试的方法是:

preg_replace('/\r\n?/', "<br />", $str);
eregi_replace(char(13), "<br />", $str);
str_replace("\r\n", "<br />", $str);
str_replace("\n", "<br />", $str);

和nl2br功能。

我看了关于谷歌的答案约半小时,没有发现任何东西。 任何人都可以帮忙吗?

Answer 1:

从php.net相当好榜样文档

// Order of replacement
$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';

// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);


Answer 2:

你的正则表达式逃避你的rn

代替

preg_replace('/\r\n?/', "<br />", $str);

尝试这个:

preg_replace('/\\r\\n/', "<br />", $str);


Answer 3:

你测试它这样的吗?

$str = str_replace( "\r\n", "<br />", $str );
$str = str_replace( "\r", "<br />", $str );
$str = str_replace( "\n", "<br />", $str );

这应该工作几乎总是。 请记住经常使用"\r" ,而不是'\r'



文章来源: Replace carriage return in an email
标签: php html xhtml