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.
remove (some) tags
Moderator: General Moderators
Re: remove (some) tags
Almost done. Just need to keep the <img> tag... Any help?
Code: Select all
$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;-
cpetercarter
- Forum Contributor
- Posts: 474
- Joined: Sat Jul 25, 2009 2:00 am
Re: remove (some) tags
Your two posts seem to say different things. Which are you trying to do - remove only the image tag; or remove all other tags but keep the image tag?
Re: remove (some) tags
Code: Select all
<?php
$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, '$1', $str);
echo $output;