2017-02-03 62 views
1

这似乎是一个非常简单的练习,但它只是没有为我工作。PHP preg_replace匹配两个换行符

我有一个文本文档,并觉得一个简单的方法来HTMLify它是使用快速正则表达式来翻译2个新行为</p><p>,这是足够简单,我所需要的。

所以;我正则表达式:

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

作品regex101.com,我通常试制出我的正则表达式。但在我的PHP它不工作;

$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>"; 

我输入:

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. 

但我对PHP的输出是:

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

我缺少什么?我敢肯定,这很简单,但我不明白为什么它的作品的网站上却没有关于我非常简单的脚本: -/

我自己也尝试了PHP_EOL和各种代码的其他排列替换\n,没有成功。

+0

你不必'\ r \ n's你呢? '\ R \ h * \ R +' –

+0

如何,但代码在regex101链接上的相同文本上工作。 – Martin

+0

@bobblebubble是,\ R工作。欢呼声,坚持它在一个答案:) – Martin

回答