2016-05-01 32 views
0

我有一個像下面正則表達式刪除一切從一些標記開始和結束於其他一些標籤

<img alt="rlogo" src="https://something.net/logo.gif/resized_logo.png?r=3" /> 

<p> 
    <strong>Headquarters:</strong> Austin, TX 
    <br /><strong>URL:</strong> <a href="https://something.com/F402B805CC">https://something.com/j/F402B805CC</a> 
</p> 

Lorem ipsum dollar sit amet 

我想刪除除「Lorem存有美元坐阿梅特」的一切,一個字符串到目前爲止,我設法去除圖像使用

preg_replace('<img alt=\"rlogo\".*>','',$description) 

標籤但<p>標籤同樣不起作用,因爲有後<p>標籤新的生產線。

有沒有辦法可以去除一切從<img開始至</a></p>

回答

2

使用s選項(點匹配換行符);

$result = preg_replace('%<img.*?</p>%si', '', $description); 

正則表達式Explanantion

<img.*?</p> 

Options: Case insensitive (i); Exact spacing; Dot matches line breaks (s); ^$ don’t match at line breaks; Greedy quantifiers; Regex syntax only 

Match the character string 「<img」 literally (case insensitive) «<img» 
Match any single character «.*?» 
    Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» 
Match the character string 「</p>」 literally (case insensitive) «</p>» 
+0

由於它的工作, –

+0

非常歡迎@ www.amitpatil.me –

相關問題