正则表达式删除从某个标签开始并以其他标签结束的所有内容
Regex remove everything starting from some tag and ends at some other tag
我有如下字符串
<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>
的所有内容
使用s
选项(点匹配换行符);
$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>»
我有如下字符串
<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>
使用s
选项(点匹配换行符);
$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>»