我嘗試更換以下標記及其內容與空字符串:優化正則表達式:重複出現次數
<a href="http://localhost/photo/448e7d40ed468d73c5f9caba573f6273-0.png" class="wall-image-anchor" target="_blank"><img src="http://localhost/photo/448e7d40ed468d73c5f9caba573f6273-0.png" /></a>
注意裏面<a>
標籤href網址可以是任何東西。所以<a>
內部的內容,在這種情況下<img>
及其內容。
到目前爲止,我得到了下面的代碼:
$text = preg_replace('@(.*?)<(?:a\b.*?class="wall-image-anchor".*?)>.*?</a>(.*?)@si', '$1$2', $text);
此代碼應變換以下字符串:
zzzzz<a href="http://localhost/zz/photo/448e7d40ed468d73c5f9caba573f6273-0.png " class="wall-image-anchor" target="_blank"><img src="http://localhost/zz/photo/448e7d40ed468d73c5f9caba573f6273-0.png" alt="Image/photo" /></a>ffff<br /><a href="http://localhost/ada/photo/448e7d40ed468d73c5f9caba573f6273-0.png " class="wall-image-anchor" target="_blank"><img src="http://localhost/ada/photo/448e7d40ed468d73c5f9caba573f6273-0.png" alt="Image/photo" /></a>ffffgg ffff<br /><a href="http://localhost/dad/photo/448e7d40ed468d73c5f9caba573f6273-0.png " class="wall-image-anchor" target="_blank"><img src="http://localhost/dad/photo/448e7d40ed468d73c5f9caba573f6273-0.png" alt="Image/photo" /></a>ffffgg'
到:
zzzzzffff
ffffgg ffff
ffffgg
此代碼的工作。我的問題是:有沒有其他辦法可以讓它更快?
問候
也許'回聲用strip_tags(str_replace函數( '', 「\ n」,$弦));'?如果這是用於瀏覽器的,請在'strip_tags'後面使用'nl2br'。或者,您可以使用解析器。至於速度,你將不得不運行測試。或者我誤解了這一點,並且你想保留'
'(你的「進入」示例沒有這些)。 'echo strip_tags($ string,'
');' – chris85