正则表达式从某个标签开始删除所有内容,并以其他标签结束

时间:2016-05-01 06:59:13

标签: php regex preg-replace

我有一个如下所示的字符串

<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 ipsum dollar sit amet”之外的所有内容,到目前为止我设法使用

删除了图片代码
preg_replace('<img alt=\"rlogo\".*>','',$description)

但同样不适用于<p>代码,因为<p>代码后面有新行。

我可以删除从<img</a></p>

的所有内容

1 个答案:

答案 0 :(得分:2)

使用s选项(Dot匹配换行符);

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

正则表达式解释

<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>»