2009-09-01 44 views

回答

5

事情是這樣的:

preg_match('/(<br>\s*){3}/i', $str, $matches); 

這比你的例子更寬鬆一點 - 它不區分大小寫的匹配和<br> S,不只是換行之間的任何空白匹配。

要代替匹配3個或更多的3:

preg_match('/(<br>\s*){3,}/i', $str, $matches); 
+0

是不是他試圖讓
標籤之間的文本? – 2009-09-01 19:25:25

+0

我並非試圖取代所有
標籤。只要其中出現3次連續的那些 – 2009-09-01 19:25:42

+0

@Yannick 我不想在標籤之間的文本,我只是想用我自己的格式 – 2009-09-01 19:26:28

3

如果你只是想更換<BR>情況下,那麼你最好做一個字符串替換。正則表達式要快得多。

$newstr = str_replace('<BR>', 'replacement...', $str); 
1

我對其採取

<?php 

$html = <<<HTML 
<BR> 
<BR> 
<BR> 
<p>^^ Replace 3 consecutive BR tags with nothing</p> 
<BR> 
<BR> 
<p>^^ those should stay, there's only 2 of them</p> 
<BR> 
    <BR> 


     <BR> 
<p>^^ But those should go, whitespace and newlines shouldn't matter 
HTML; 

echo preg_replace("/(?:<br>\s*){3}/i", '', $html);