Apply striptags() selectively on <a> 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
saudepp
Forum Newbie
Posts: 4
Joined: Thu Dec 16, 2010 3:29 am

Apply striptags() selectively on <a> tags

Post by saudepp »

Hi there,
I want to apply the striptags() function selectively on <a> tags containing some specific pattern in the URL.

e.g. I have text that contains two different sorts of <a> tags:

sort 1:
<a href=/dirname1/somepage>something</a>

sort 2:
<a href=/dirname2/somepage>something</a>

note that "somepage" and "something" can vary from tag to tag. the different URL sorts can be distinguished by dirname1 or dirname2.

now, I want to scan the text and remove all <a> tags pointing to URLs that begin with /dirname1, while I want to keep all other tags.
after removal of sort 1 tags, only the anchors "something" should be left over.

how could this be done?

thanks for your help.

-saudepp
saudepp
Forum Newbie
Posts: 4
Joined: Thu Dec 16, 2010 3:29 am

Re: Apply striptags() selectively on <a> tags

Post by saudepp »

nobody has a clue?
please help ...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Apply striptags() selectively on <a> tags

Post by pickle »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:4. All users of any level are restricted to bumping (as defined here) any given thread within twenty-four (24) hours of its last post. Non-trivial posts are not considered bumping. A bump post found in violation will be deleted, and you may or may not receive a warning. Persons bumping excessively be considered as spammers and dealt with accordingly.
strip_tags() does allow you to specify a whitelist of allowed tags. If you only want to strip links, you need to specify every tag except the <a> tag.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
saudepp
Forum Newbie
Posts: 4
Joined: Thu Dec 16, 2010 3:29 am

Re: Apply striptags() selectively on <a> tags

Post by saudepp »

please read my question carefully, it's not as simple as that ...
<a> shall be removed conditional on the URL structure.

some <a> tags should survive, some other <a> tags shouldn't.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Apply striptags() selectively on <a> tags

Post by Darhazer »

saudepp wrote:please read my question carefully, it's not as simple as that ...
<a> shall be removed conditional on the URL structure.

some <a> tags should survive, some other <a> tags shouldn't.
use preg_replace
saudepp
Forum Newbie
Posts: 4
Joined: Thu Dec 16, 2010 3:29 am

Re: Apply striptags() selectively on <a> tags

Post by saudepp »

thanks, I am already playing with preg_replace and regular expressions.
nice, but not so easy to learn ...

cheers

I wann use it on this page: Wellnessoase
Post Reply