2012-04-02 37 views
0

So, I have a string of text:替換某些<a href..certain.domain.com... with php preg_replace

string with a bunch of links but i need to simply get rid of these <a href="a.domain.com">Title</a> string with more links and text 

I need to get rid of everything from <a to a> where a.domain.com is present (its always the same domain) using PHP preg_replace.
It seems so simple but I can't figure this out >.< or find anywhere that explains regex :/

回答

1

我總是很喜歡這個網站的正則表達式:http://www.regular-expressions.info/ 這不是一個完整的答案,但我只想匹配<a [...]>[...]</a>,並確保它不貪婪,所以你最終不會從<a [...]>第一次出現撕裂一切一直到最後的</a>。 這在另一個論壇上建議,你可能想試一試,以及:

$rev= preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $_POST['rev']); 
0

If you're looking to keep what's inside the <a> tags, you can try the strip_tags()函數。

如果你想更復雜的東西,偉大的教程學習正則表達式是http://www.regular-expressions.info/

您可以嘗試像

$no_tags = preg_replace('#<a (.*?)>(.*?)</a>#', '', $text); 

,但請注意,這將打破,如果你的代碼是不是更復雜您發佈的示例。

+0

不,它只是需要刪除整個事情 網站看起來不錯壽 – King 2012-04-02 21:16:09