0

I have a string like below

<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  

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 <p> tag because there is new line after <p> tag.

Is there way i can remove everything starting from <img till </a></p>

www.amitpatil.me
  • 3,001
  • 5
  • 43
  • 61

1 Answers1

2

Use the s option (Dot matches line breaks);

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

Regex 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>»
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268