2010-03-27 91 views
1

我要全部替換 」的mailto:與普通的電子郵件中的HTML「 鏈接保換的<a href =「電子郵件地址:只用電子郵件aadress

In: text .... <a href="mailto:[email protected]">not needed</a> text 
Out: text .... [email protected] text 

我這樣做:

$str = preg_replace("/\<a.+href=\"mailto:(.*)\".+\<\/a\>/", "$1", $str); 

但如果在字符串或HTML內多個電子郵件「一」的標籤

In: <a href="mailto:[email protected]">not needed</a><a href="mailto:[email protected]"><font size="3">[email protected]</font></a> 
Out: [email protected]"> 

回答

3

讓你的對手失敗非貪婪?到量詞+*爲:

$str = preg_replace("/\<a.+?href=\"mailto:(.*?)\".+?\<\/a\>/", "$1", $str); 

你也不必逃避<>和因爲有一些/在模式它能夠更好地使用不同的分隔符,因爲你沒有做任何變量圖案內插值,也沒有必要這樣就可以避免逃逸"格局內括在"

$str = preg_replace('#<a.+?href="mailto:(.*?)".+?</a>#', "$1", $str); 
+0

謝謝,不貪不匹配的工作做好感謝您的逃脫信息。 – Lauri 2010-03-27 15:57:23

相關問題