我想在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'
。