0

Trying to remove only gif image from the content , tried below but that does remove all images

  $content = "this is <img src="https://ecwowocc.com/test.gif"/> something with an <img src=https://ecwowocc.com/test.png"/> in it.";
    $content = preg_replace("/<img[^>]+\>/i", "(image) ", $content); 
    echo $content;

i want to remove only GIF from the content

  • you need something which matches the exact text "gif" in the middle of the img tag then. What have you tried so far? – ADyson Aug 12 '20 at 09:14
  • 2
    its 2020 do people still use regex to parse html :/ [see why its not a good idea](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Lawrence Cherone Aug 12 '20 at 09:39
  • @LawrenceCherone You are totally right of course, but in the OP's case, it would be a bit overkill to implement an HTML parser to detect a single self-closing tag like `img`. There are no cases of nested elements possible here and from the question it is not expected that we can have a `>` in the image path. – Kaddath Aug 12 '20 at 09:53

2 Answers2

1

You can try with:

<img[^>]+src=\\?(?:"|')[^.]+\.gif\\?(?:"|')[^>]+\>

specifics:

  • will match src attribute either used with " or '

  • will match if " or ' is escaped or not

  • small caution: this regex will not support a > character in another attribute of the img tag

see it in Regex101

As suggested by AmigoJack, it can be improved to the form:

<img[^>]+src=(\\?["'])[^'"]+\.gif\1[^>]*\/?>

Kaddath
  • 5,933
  • 1
  • 9
  • 23
  • i have image src so what do change in regrex? –  Aug 13 '20 at 07:12
  • 1
    Why `(?:"|')` and not `["']`? Why only `[^.]` and not `[^"'>]`? Why is `>` escaped? To me this is quite trite. A better regex (not "regrex") is `]+src(\\?["'])[^'">]+\.gif\1[^>]*/?>` – AmigoJack Aug 13 '20 at 20:36
  • @AmigoJack for the first I reckon it's a bad habit I took, classes are effectively quicker than alternations, for the rest, there are ideas, but for a trite thing your regex is both invalid (`/` needs to be escaped) and [doesn't actually work](https://regex101.com/r/7COeZl/2). I added your suggestions with corrections though. – Kaddath Aug 17 '20 at 12:36
  • No, `/` doesn't **need** to be escaped (only when you use `/` as regex delimiters, but that's not mandatory). Yes, my version didn't work, but [`]+src=(["'])\\?[^'">]+\.gif\1[^>]*/?>`](https://regex101.com/r/7COeZl/2) does (even better, because yours would also match ``. Again: your `\>` makes no sense (neither PHP strings nor regex need it escaped). – AmigoJack Aug 17 '20 at 12:45
  • Please be more precise, neither [the original](https://regex101.com/r/H3J4XI/1) not the [updated one](https://regex101.com/r/QuP7w0/1) will match `` to fail because it's not a .gif. Agreed for the `\>`, the backslash was a leftover from a precedent regex. But actually it's still valid – Kaddath Aug 17 '20 at 12:52
0

Try Using str_replace() function

Syntax,

str_replace(search, replace, subject);

<?php
  $content = "this is <img src=\"test.gif\"/> something with an <img src=\"test.png\"/> in it.";
  echo str_replace(".gif", ".png", $content);
?>
meewog
  • 1,690
  • 1
  • 22
  • 26
DevSavata
  • 51
  • 5
  • This will replace all `.gif` image extensions by `.png`, which will not remove the gif images and will most probably lead to images not found icons – Kaddath Aug 12 '20 at 09:35