如何替换字符串中的一个或两个连续换行符?
我正在 PHP 中开发一个单一服务站点,它只显示以下消息由访问者发布(最好围绕网站主题)。 任何人每小时最多可以发布三条消息。
由于该网站只有一页,我想控制每条消息的垂直长度。 但是,我确实希望至少部分保留原始消息中的换行符。 一种折衷方案是允许有两个换行符,但如果有两个以上换行符,则将其替换为连续两个换行符。 Stack Overflow 就实现了这一点。
例如:
Porcupines\nare\n\n\n\nporcupiney.
将更改为
Porcupines<br />are<br /><br />porcupiney.
检查换行符的一个棘手方面是它们可能被收集并存储为 \r\n
、\r
或 \n
。 我考虑过使用 nl2br() 将所有换行符转换为
,但这似乎没有必要。
我的问题:在 PHP 中使用正则表达式(使用 preg_match()
和 preg_replace()
等函数),如何检查连续两个以上换行符的实例(它们之间有或没有空格)然后将它们更改为总共两个换行符?
I'm developing a single serving site in PHP that simply displays messages that are posted by visitors (ideally surrounding the topic of the website). Anyone can post up to three messages an hour.
Since the website will only be one page, I'd like to control the vertical length of each message. However, I do want to at least partially preserve line breaks in the original message. A compromise would be to allow for two line breaks, but if there are more than two, then replace them with a total of two line breaks in a row. Stack Overflow implements this.
For example:
Porcupines\nare\n\n\n\nporcupiney.
would be changed to
Porcupines<br />are<br /><br />porcupiney.
One tricky aspect of checking for line breaks is the possibility of their being collected and stored as \r\n
, \r
, or \n
. I thought about converting all line breaks to <br />
s using nl2br()
, but that seemed unnecessary.
My question: Using regular expressions in PHP (with functions like preg_match()
and preg_replace()
), how can I check for instances of more than two line breaks in a row (with or without blank space between them) and then change them to a total of two line breaks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为类似的东西
应该有效。 虽然我不太记得 PHP 语法,但它可能需要更多转义:-/
Something like
should work, I think. Though I don't remember PHP syntax exactly, it might need some more escaping :-/
\R
与系统无关 转义序列 将匹配\n
、\r
和\r\n
。因为您想要贪婪地匹配 1 或 2 个连续换行符,所以需要使用限制量词
{1,2}
。代码:(演示)
输出:
现在,澄清其他答案不正确的原因/位置...
@DavidZ's由于量词表达式不正确,无法解释的答案无法替换单独的换行符(失败演示)。
它生成:
@chaos 的纯代码答案可以生成完全相同的结果(失败演示)。 正则表达式不仅冗长且错误地实现了量词逻辑,而且还添加了
s
模式修饰符。如果模式中存在点元字符,则
s
模式修饰符仅对正则表达式产生影响。 由于模式中没有.
,因此修饰符毫无用处,并且正在向研究人员传授无意义/不正确的编码实践。\R
is the system-agnostic escape sequence which will match\n
,\r
and\r\n
.Because you want to greedily match 1 or 2 consecutive newlines, you will need to use a limiting quantifier
{1,2}
.Code: (Demo)
Output:
Now, to clarify why/where the other answers are incorrect...
@DavidZ's unexplained answer fails to replace the lone newline character (Demo of failure) because of the incorrect quantifier expression.
It generates:
The exact same result can be generated by @chaos's code-only answer (Demo of failure). Not only is the regular expression long-winded and incorrectly implementing the quantifier logic, it is also adding the
s
pattern modifier.The
s
pattern modifier only has an effect on the regular expression if there is a dot metacharacter in the pattern. Because there is no.
in the pattern, the modifier is useless and is teaching researchers meaningless/incorrect coding practices.我只是想补充一点,尽管它没有直接回答问题,但它可能会帮助那些想要限制换行次数的人。
我需要它来限制论坛帖子中的换行次数。 我使用了上面选定的答案,并添加了以下内容:
无论如何,这都会将换行符的数量限制为最大数量。
I just wanted to add to this, even though it doesnt directly answer the question, it may help someone who is wanting to limit the number of line breaks.
I needed this to limit the number of line breaks in forum posts. I used the selected answer above, and added this:
This limits the number of line breaks to a maximum amount no matter what.