简体   繁体   中英

Replace single line break with double line break

I have a string:

$str =
'hello
world';

And I want to replace single line breaks to double line breaks, so we have:

$str =
'hello

world';

I tried:

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

But it did not work. What's the correct approach? Can we use PHP_EOL ...?

To don't think about EOL format, you can use regex to find and clone it. \\R works with any EOL char combination

$str = preg_replace("/(\R)/", "$1$1", $str);

demo

您的方法有效,但您需要一个纯文本/文本输出标题才能在浏览器上查看它。

header('Content-Type:text/plain');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM