2017-02-03 113 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

回答