2013-02-22 73 views
0

我有以下字符串,我想下面的標籤,包括那些標籤之間移除一切:的preg_replace字符串標籤

<br> and the </span> 

<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteria<br><span class='attribute'> - University of Maryland</span></a> 

我已經試過preg_replace('/<br>.*?</a>/', '', $link)但似乎去除HREF ...

任何想法我應該怎麼做?

編輯: 使用後:

preg_replace('/<br>.*?<\/span>/', '', $link) 

我現在看到源:

<tr> 
    <td><a class='interactive' href='http://www.proteinatlas.org/' target='_blank' alt='The protein atlas' >The protein atlas<br><span class='attribute'> - Uppsala Univeristät</td> 
    <td width='16' align='center' valign='middle'><a class='delete_link' href='#' data_link='%3Ca+class%3D%27interactive%27+href%3D%27http%3A%2F%2Fwww.proteinatlas.org%2F%27+target%3D%27_blank%27+alt%3D%27The+protein+atlas%27+%3EThe+protein+atlas%3Cbr%3E%3Cspan+class%3D%27attribute%27%3E+-+Uppsala+Univerist%C3%A4t' data_topic='161' data_introduction=''><img src="../images/delete.png" width="16" height="16" alt="delete" title="delete this link" border='0' /></a></td> 
    </tr> 
    <tr> 
    <td> funded by the Knut and Alice Wallenberg Foundation</span></a></td> 
    <td width='16' align='center' valign='middle'><a class='delete_link' href='#' data_link='+funded+by+the+Knut+and+Alice+Wallenberg+Foundation%3C%2Fspan%3E%3C%2Fa%3E' data_topic='161' data_introduction=''><img src="../images/delete.png" width="16" height="16" alt="delete" title="delete this link" border='0' /></a></td> 
    </tr> 

編輯:也試過;

preg_replace('/<br><span class=\'attribute\'>.*?<\/span>/', '', $link) 

但問題依然存在。

EDIT

仍然看到源表示爲:

<a class='interactive' href='http://www.tinyurl.com/immunologygame/' target='_blank' alt='Innate Immunology game' >Innate Immunology game<br><span class='attribute'> - University of Ballarat</span></a> 
+0

如果主題字符串中有一些換行符,則嘗試將「s」修飾符添加到正則表達式中:'/
。*? <\/span>/s' – Placido 2013-02-22 08:52:27

回答

0

只要使用這個空頭格局:

/<br>.*?<\/span>/ 

輸出將是這樣的:

<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteriabla</a> 
+0

我原以爲這樣可以工作,但span標籤仍然顯示:我已經用顯示的源編輯了OP。 – IlludiumPu36 2013-02-22 08:09:09

+0

實際上我可以看到它的工作原理,但是在跨度標籤之間的內容中有一個逗號時會出現問題......我該怎麼逃避它? – IlludiumPu36 2013-02-22 08:31:47

+0

嗯,即使在span標籤之間手動刪除文本中的任何逗號,我仍然可以在源代碼中看到br和span標籤...請參閱編輯OP – IlludiumPu36 2013-02-22 08:44:04

1

嘗試這種情況:

<?php 

$str = "<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteria<br><span class='attribute'> - University of Maryland</span></a>"; 

$r = '/<br>(.+?)<\/span>/'; 

$str = preg_replace($r, '', $str); 

echo $str; 

?> 

輸出:

<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteria</a> 

演示:http://regexr.com?33s84

+0

我認爲你的模式中的組在他的情況下是沒有必要的。 – tuxtimo 2013-02-22 07:51:53

+0

@tuxtimo呃,其實不是。我只是爲了讓一個正則表達式更直觀地「揭示」和解釋性的(就某種方式來說,這是一種類似於這個或者非捕獲類的類型(這會更好地說實話))。 )。 – 2013-02-22 07:53:26

1

試試這個

$str = "<a class='interactive' href='http://mathbench.umd.edu/modules/microbio_counting-methods/page01.htm' target='_blank' alt='Counting bacteria' >Counting bacteria<br><span class='attribute'> - University of Maryland</span></a>"; 

echo htmlspecialchars(preg_replace('#(<a[^>]+?>)([^<>]+).*#i', '$1$2</a>', $str));