将多行转换为单行

时间:2019-10-07 15:41:40

标签: php

我想将一条线从几行变成一行:

$text = 'One line
Two line';

我尝试了替换,但是没有用:

$text = str_replace('
', '; ', $text );

我希望字符串变成:

$text = 'One line; Two line';

1 个答案:

答案 0 :(得分:0)

CR,LF还是两者都用作行尾取决于相应的编辑器。正则表达式中的“ \ R”适合所有情况。

$text = 'One line
Two line';

$textInOneLine = preg_replace('~\R~','; ',$text);

echo '<pre>'.$textInOneLine.'</pre>';

输出:

One line; Two line