input
<a href="http://mysite.com">My Site</a>
<a href="http://mysite.com/image.jpg"><img src="http://mysite.com/image.jpg"/></a>
<a href="http://mysite.com/image.gif"><img src="http://mysite.com/image.gif"/></a>
<a href="http://yoursite.com">Your Site</a>
output
<a href="http://mysite.com/image.jpg"><img src="http://mysite.com/image.jpg"/></a>
<a href="http://mysite.com/image.gif"><img src="http://mysite.com/image.gif"/></a>
Thank's for help
I am not a PHP developer,but I can give you a javascript demo,hope it can give you some help :)
var reg=/<a\b[^>]*?href=\"((?!jpg|gif|png).)*?"[^>]*?>.*?<\/a>/gi;
yourstr=yourstr.replace(reg,'');
This will skip over all the other attributes in the anchor tag, even if they have a value which would look like an attribute nested in the value.
<a(?=\s|>) # validate this is an anchor tag
(?! # start look ahead ! must not contain, = must contain
(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*? # move through tag, skipping over quoted or non quoted values
\shref="[^"]*(?:jpg|png|gif)" # find href, capture value including quotes if they exist
) # end look ahead
[^>]*>.*?<\/a> # capture the entire to the close tag
Sample Text
note the second line
<a href="http://mysite.com">My Site</a>
<a wrongtag=" href='http://mysite.com/image.jpg' " href="http://mysite.com">My Site</a>
<a href="http://mysite.com/image.jpg"><img src="http://mysite.com/image.jpg"/></a>
<a href="http://mysite.com/image.gif"><img src="http://mysite.com/image.gif"/></a>
<a href="http://yoursite.com">Your Site</a>
Code
<?php
$sourcestring="your source string";
echo preg_replace('/<a(?=\s|>)
(?! # start look ahead ! must not contain, = must contain
(?:[^>=]|=\'[^\']*\'|="[^"]*"|=[^\'"][^\s>]*)*? # move through tag, skipping over quoted or non quoted values
\shref="[^"]*(?:jpg|png|gif)" # find href, capture value including quotes if they exist
) # end look ahead
[^>]*>.*?<\/a> # actually capture the string
/imsx','',$sourcestring);
?>
Matches
[0] => <a href="http://mysite.com/image.jpg"><img src="http://mysite.com/image.jpg"/></a>
[1] => <a href="http://mysite.com/image.gif"><img src="http://mysite.com/image.gif"/></a>