【问题标题】:Convert String with Line Breaks into Single Line With Line Breaks Still Intact将带有换行符的字符串转换为单行且换行符仍然完好无损
【发布时间】:2015-02-04 01:24:06
【问题描述】:

我有以下字符串:

Hello Eric,

Thank you very much for the links. This was exactly what I needed.

Greetings,
John

我正在使用 php 的 nl2br 函数将新行转换为正常工作的换行符。在我的字符串上运行nl2br 函数后,它变成了这样:

Hello Eric,<br />

Thank you very much for the links. This was exactly what I needed.<br />

Greetings,<br />
John

到目前为止,这就是我想要实现的目标。但是,我现在需要将带有换行符的新字符串转换为一行:

Hello Eric,<br />Thank you very much for the links. This was exactly what I needed.<br />Greetings,<br />John

我尝试了几种不同的方法,但似乎都没有奏效。以下是我尝试过的一些事情:

$str = str_replace(array("\r","\n"),"",$str);

$str = str_replace(array("\r\n", "\n", "\r"), "", $str);

$str = preg_replace( "/\r|\n/", "", $str);

但这些都不适合我。

【问题讨论】:

标签: php html string


【解决方案1】:

试试这个:

$str = str_replace(array("\r\n","\n"),"",$str);

代替

$str = str_replace(array("\r","\n"),"",$str);

可以在以下位置观看: http://sandbox.onlinephpfunctions.com/code/41a6fbd2c937418fce927b31f6ab74ad30f8b4d0

在此处阅读有关换行符的更多信息: http://en.wikipedia.org/wiki/Newline#Representations

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 2021-10-08
    相关资源
    最近更新 更多