2016-05-01 64 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 –

相关问题