PHP preg_replace匹配两个换行符

时间:2017-02-03 22:07:25

标签: php regex

这似乎是一个非常简单的练习,但它对我来说并不适合。

我有一个文本文档,并且觉得HTML化的一种简单方法是使用快速正则表达式将2个换行符转换为require "rubygems" require "httparty" response = HTTParty.post("https://fake-heroku-94488.herokuapp.com/api/v1/sign-in", :body => {:user_login => {:email => "fake@email.com", :password => "password123"}}) puts response.body, response.code, response.message, response.headers.inspect ,这对于我需要的内容来说非常简单。

因此;我的正则表达式:

</p><p>

regex101.com中的 ,我通常会试用我的正则表达式。但在我的PHP中,它无法正常工作;

\n\h*\n+
/** This matches the newline, 
    then any number (zero or more) whitespace 
    and then a second new line. **/ 

我的意见:

$text = trim($_POST['text']);
$text = strip_tags($_POST['text']);

/*** Find new Paragraphs ***/
$text = preg_replace("/\n\h*\n/","</p>\n<p>",$text);
$text = "<p>".$text."</p>";

但我在PHP上的输出是:

30th Sep 16

Day 176 has seen more golf balls struck with growing frustration as I try
to train the brain, a short swim, and a dawning realisation which has led
to probably the biggest question that I have ever posed.

<p>30th Sep 16 Day 176 has seen more golf balls struck with growing frustration as I try to train the brain, a short swim, and a dawning realisation which has led to probably the biggest question that I have ever posed.</p> 添加$count var会返回零,但未找到匹配项。

预期产出:

preg_replace

我错过了什么?我确定它很简单,但我不明白它为何在网站上运行,而不是在我非常简单的脚本上: - /

我也尝试用<p>30th Sep 16 </p> <p>Day 176 has seen more golf balls struck with growing frustration as I try to train the brain, a short swim, and a dawning realisation which has led to probably the biggest question that I have ever posed.</p> 代替\n以及其他各种代码排列,但没有成功。

1 个答案:

答案 0 :(得分:1)

可能是您有\r\n个换行符。对于任何换行序列,请尝试escape sequence \R

\R\h*\R+

See demo at regex101PHP Demo at eval.in