1

i want remove all img tag in a string if don`t like my form but i can't it

example (i want to stay in my string) :

<img src="http://localhost/uploads/user1/test3.jpg" title="test" class="img-responsive img-maxheight">
<img src="http://localhost/uploads/user2/test1.jpg" alt="test" class="img-responsive img-maxheight">
<img src="http://localhost/uploads/user3/test2.jpg" class="img-responsive img-maxheight">

and my regex code in php is :

$pattern ="/<img src=\"http:\/\/localhost\/(uploads|thumbs)\/$user\/(.*?)\" (alt|title|)=\"(.*?)\" class=\"img-responsive img-maxheight\">/i";

$replacement = ' ';
$content = preg_replace($pattern, $replacement, $content);
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • 1
    Why use regex? ? – Rotimi Apr 23 '17 at 12:00
  • Possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Imanuel Apr 23 '17 at 12:11

1 Answers1

0

The shortest version of a possible pattern would be /<img[^>]*>/.
This will probably work most of the time. A bit better would be this: /<\simg[^>]*>/. It will work with < img ......>, too.
You can insert very many corner cases to improve the pattern, but the better solution would be not to use a regex in the first place.
Instead, you should be parsing the html by dedicated parsers: How do you parse and process HTML/XML in PHP?

Community
  • 1
  • 1
Imanuel
  • 3,596
  • 4
  • 24
  • 46