In the following HTML content
<a href="www.google.com">Google</a> blah blah blah... <a href="www.bing.com"><img src="a.gif></a>
How can I remove image links only? Need to keep others links...
Any ideas? Thanks.
do you mean you want to remove the <a> tag of the link as well as the <img> tag?
Maybe this:
$str = '<a href="www.google.com">Google</a> blah blah blah... <a href="www.bing.com"><img src="a.gif></a> '; $pattern = '/<a[^>]+><img[^>]+><\/a>/i'; $output = preg_replace($pattern, '', $str); echo $output;
Thanks... But I need to keep the <img> tag, your code is deleting everything.
$pattern = '/<a[>]+><img[>]+><\/a>/i'; $output = preg_replace($pattern, '', $str); echo $output;
Assuming you do not run into strings like
<a ...>text and <img ...></a>
$p = '#<a[^>]*?>(<img[^>]*?>)</a>#'; $out = preg_replace($p, '$1', $str);
Try johana's code, that should work.