如何替换字符串中的一个或两个连续换行符?

发布于 2024-07-18 20:47:58 字数 738 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回心转意 2024-07-25 20:47:59
preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $text)
preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $text)
北城孤痞 2024-07-25 20:47:59

我认为类似的东西

preg_replace('/(\r|\n|\r\n){2,}/', '<br/><br/>', $text);

应该有效。 虽然我不太记得 PHP 语法,但它可能需要更多转义:-/

Something like

preg_replace('/(\r|\n|\r\n){2,}/', '<br/><br/>', $text);

should work, I think. Though I don't remember PHP syntax exactly, it might need some more escaping :-/

多像笑话 2024-07-25 20:47:59

\R 与系统无关 转义序列 将匹配 \n\r\r\n

因为您想要贪婪地匹配 1 或 2 个连续换行符,所以需要使用限制量词 {1,2}

代码:(演示)

$string = "Porcupines\nare\n\n\n\nporcupiney.";

echo preg_replace('~\R{1,2}~', '<br />', $string);

输出:

Porcupines<br >are<br /><br />porcupiney.

现在,澄清其他答案不正确的原因/位置...

@DavidZ's由于量词表达式不正确,无法解释的答案无法替换单独的换行符(失败演示)。

它生成:

Porcupines\nare<br/><br/>porcupiney.

@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)

$string = "Porcupines\nare\n\n\n\nporcupiney.";

echo preg_replace('~\R{1,2}~', '<br />', $string);

Output:

Porcupines<br >are<br /><br />porcupiney.

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:

Porcupines\nare<br/><br/>porcupiney.

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.

要走就滚别墨迹 2024-07-25 20:47:59

我只是想补充一点,尽管它没有直接回答问题,但它可能会帮助那些想要限制换行次数的人。
我需要它来限制论坛帖子中的换行次数。 我使用了上面选定的答案,并添加了以下内容:

//Some pre processing
$textarea_reply = str_replace("\r", "<br>", $textarea_reply);
$textarea_reply_splitByLines = explode("<br>", $textarea_reply);
$textarea_reply = "";
$line_count = 0;
$line_limit = 10;

//Re-add the line breaks with a limit of $line_limit
foreach ($textarea_reply_splitByLines as $line){
    $textarea_reply.= $line." "; 
    if($line_count<$line_limit) $textarea_reply.= "<br>";
    $line_count++;
}

无论如何,这都会将换行符的数量限制为最大数量。

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:

//Some pre processing
$textarea_reply = str_replace("\r", "<br>", $textarea_reply);
$textarea_reply_splitByLines = explode("<br>", $textarea_reply);
$textarea_reply = "";
$line_count = 0;
$line_limit = 10;

//Re-add the line breaks with a limit of $line_limit
foreach ($textarea_reply_splitByLines as $line){
    $textarea_reply.= $line." "; 
    if($line_count<$line_limit) $textarea_reply.= "<br>";
    $line_count++;
}

This limits the number of line breaks to a maximum amount no matter what.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文