-2
$str=<<<EOT
<img src="./img/upload_20571053.jpg" /><span>some word</span><div>some comtent</div>
EOT;

How to remove the img tag with preg_match_all or other way? Thanks.

I want echo <span>some word</span><div>some comtent</div> // may be other html tag, like in the $str ,just remove img.

fish man
  • 2,666
  • 21
  • 54
  • 94
  • Possible duplicate: http://stackoverflow.com/questions/1107194/php-remove-img-tag-from-string – Jeroen Jun 21 '11 at 13:40
  • 1
    You shouldn’t use a blacklist (remove forbidden) but a whitelist (only keep allowed). Use a proper HTML sanitizer instead. – Gumbo Jun 21 '11 at 13:40
  • @Jeroen Offerijns: Bad advice. See my comment to [Karo96’s answer](http://stackoverflow.com/questions/6426139/preg-match-all-how-to-remove-img-tag/6426182#6426182). – Gumbo Jun 21 '11 at 13:43
  • Well it seems to be an duplicate because it has the same accepted answer.. – Jeroen Jun 21 '11 at 20:39

7 Answers7

2

As many people said, you shouldn't do this with a regexp. Most of the examples you've seen to replace the image tags are naive and would not work in every situation. The regular expression to take into account everything (assuming you have well-formed XHTML in the first place), would be very long, very complex and very hard to understand or edit later on. And even if you think that it works correctly then, the chances are it doesn't. You should really use a parser made specifically for parsing (X)HTML.

Here's how to do it properly without a regular expression using the DOM extension of PHP:

// add a root node to the XHTML and load it
$doc = new DOMDocument;
$doc->loadXML('<root>'.$str.'</root>');
// create a xpath query to find and delete all img elements
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//img') as $node) {
        $node->parentNode->removeChild($node);
}
// save the result
$str = $doc->saveXML($doc->documentElement);
// remove the root node
$str = substr($str, strlen('<root>'), -strlen('</root>'));
reko_t
  • 55,302
  • 10
  • 87
  • 77
1
preg_replace("#\<img src\=\"(.+)\"(.+)\/\>#iU", NULL, $str);
echo $str;

?

1
$str = preg_replace('#<img[^>]*>#i', '', $str);
Shef
  • 44,808
  • 15
  • 79
  • 90
1

In addition to @Karo96, I would go more broad:

/<img[^>]*>/i

And:

$re = '/<img[^>]*>/i';
$str = preg_replace($re,'',$str);

demo

This also assumes the html will be properly formatted. Also, this disregards the general rule that we should not parse html with regex, but for the sake of answering you I'm including it.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
0

Perhaps you want preg_replace. It would then be: $str = preg_replace('#<img.+?>#is', '', $str), although it should be noted that for any non-trivial HTML processing you must use an XML parser, for example using DOMDocument

Fluffy
  • 27,504
  • 41
  • 151
  • 234
0
$noimg = preg_replace('/<img[^>]*>/','',$str);

Should do the trick.

-1

Don't use regex's for this. period. This isn't exactly parsing and it might be trivial but rexeg's aren't made for the DOM:

RegEx match open tags except XHTML self-contained tags

Just use DomDocument for instance.

Community
  • 1
  • 1
hoppa
  • 3,011
  • 18
  • 21
  • I don't understand how anyone could object to my answer. Downvoter, you're plain wrong :) – hoppa Jun 21 '11 at 14:15