remove (some) tags

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

remove (some) tags

Post by pedroz »

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.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Re: remove (some) tags

Post by pedroz »

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

Post by cpetercarter »

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?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: remove (some) tags

Post by Darhazer »

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;
Post Reply