0

I would like to remove img tag completely with any attribute or styling from a string using preg_replace

$content= "image to remove completely <img src="jem.jpg" style="height:162px; width:397px" />";

This is something with in it.

I tried using preg_replace, but it didn't work.

$content= "image to remove completely <img src="jem.jpg" style="height:162px; width:397px" />";

$content = preg_replace("/<img[^>]+\>/i", " ", $content ); 

expected output: image to remove completely

Dharman
  • 30,962
  • 25
  • 85
  • 135
komay
  • 3
  • 2
  • 2
    Possible duplicate of [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – MonkeyZeus Jun 26 '19 at 18:48
  • FYI, your first line isn't going to work because of the quotes. – Patrick Q Jun 26 '19 at 18:50
  • 1
    Assuming `$content` is actually a valid string (as mentioned above, the syntax as in the OP is incorrect), that regex pattern should match that tag and the replacement should be working fine, though note that the output would be `image to remove completely ` (with two spaces at the end). – Garrett Albright Jun 26 '19 at 18:59
  • Thanks i saw this and correct still the same npt working $content= "image to remove completely "; $content = preg_replace("/]+\>/i", " ", $content ); – komay Jun 27 '19 at 06:00

0 Answers0