RegEx: remove <img which src attribute is empty

I want the regex pattern to delete images that have the src attribute empty, for example:

$html = '<img src="adasas.jpg" /><br />asasas<br />sdfsdf<br /><img title="asa" src="" />'; 

or

 $html = '<img src="adasas.jpg" /><br />asasas<br />sdfsdf<br /><a href="adafgag"><img title="asa" src="" /></a>'; 

if this <img exists between the <a> , I also want to delete everything ( <a and <img ).

I checked the code below, but it deleted all $ html

 echo preg_replace( '!(<a([^>]+)>)?<img(.*?)src=""([^>]+)>(</a>)?!si' , '' , $html ); 

Can anybody help me?

early

+4
source share
1 answer

Your problem is probably related to a lot .*? . Rather, use [^>]* , as in other parts of the template:

 '!(<a\s[^>]+>)?<img([^>]+)src=""([^>]*)>(</a>)?!i' 
+4
source

All Articles