替换换行符,但保留空白行 [英] Replace newlines, but keep the blank lines

查看:136
本文介绍了替换换行符,但保留空白行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用空格替换换行符(\ r \ n),但我想保留空白行.换句话说,如果\ r \ n之前没有另一个\ r \ n,我想用''替换\ r \ n.例如:

I want to replace newlines (\r\n) with space, but I want to keep the blank lines. In other words, I want to replace \r\n with ' ', if \r\n is not preceded by another \r\n. For example:

line 1

line 2
line 3
line 4

终端最终显示为...

Shold end up as...

line 1

line 2 line 3 line 4

但不是第1行,第2行,第3行,第4行",这就是我现在正在做的事情

But not as "line 1 line 2 line 3 line 4", which is what I'm doing right now with this

preg_replace("/\r\n/", " ", $string);

推荐答案

尝试一下:

(?<!\n)\n(?!\n)

当然,您可以将\n更改为所需的任何内容.

Of course, you can change \n to whatever you need.

工作示例: http://ideone.com/dF5L9

这篇关于替换换行符,但保留空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆