www.amitpatil.me www.amitpatil.me - 8 months ago 14
PHP Question

Regex remove everything starting from some tag and ends at some other tag

I have a string like below

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

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

Lorem ipsum dollar sit amet

I want to remove everything except "Lorem ipsum dollar sit amet", So far i managed to remove image tag using

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

But the same doesnt work for
tag because there is new line after

Is there way i can remove everything starting from


Use the s option (Dot matches line breaks);

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

Regex Explanantion


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