我正在尝试用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函数。
我在谷歌上找了大约半个小时的答案,但一无所获。有人能帮忙吗?
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);
正则表达式正在转义r
和n
。
代替
preg_replace('/'r'n?/', "<br />", $str);
试试这个:
preg_replace('/''r''n/', "<br />", $str);
你这样测试了吗?
$str = str_replace( "'r'n", "<br />", $str );
$str = str_replace( "'r", "<br />", $str );
$str = str_replace( "'n", "<br />", $str );
这应该总是有效的。记住始终使用"'r"
而不是''r'
。