www.amitpatil.me www.amitpatil.me - 2 years ago 58
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

Answer Source

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>»
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download