Jump to content

Using regex to remove link but keep anchor text


pufferz

Recommended Posts

Hi,

 

I'm still a newbie PHP coder but am making a wordpress plugin that removes all links in a post. The way I parse for the links right now it by using simple string manipulation but I have heard that regex would be a far more flexible than this. So my question:

 

How can I use regex to identify and remove the anchor tags and hyperlink, but keep the anchor text intact?

 

Any help would be appreciated

Offtopic:

> Just out of interest, why did you (Daniel0) choose [^>] as the pattern match for the anchor text?

 

Is there a place to discuss this kind of thing (choosing one way or another to do a task) which isn't in pre-existing question threads?

Offtopic:

> Just out of interest, why did you (Daniel0) choose [^>] as the pattern match for the anchor text?

 

Is there a place to discuss this kind of thing (choosing one way or another to do a task) which isn't in pre-existing question threads?

 

I suppose you can just ask in the board that belongs to that subject. There is just the distinction that app design typically is more theoretical and "PHP Coding Help" is for help with specific code.

 

Is there any particular thing you're thinking of?

Thanks very much for the help! I was able to successfully integrate the code into my plugin. However I have another unforseen problem that someone here might be able to help me with. I want to remove paintext URLs that are inside of posts. Ex. "Hey check out this video! www.video.com"  ----> "Hey check out this video! [link removed]

 

Is this possible using regex?

Are you saying that if the link takes the form of a url that it wants removing completely if not then you wish to remove the link but leave the text? If so then yes I guess that would be possible, a pattern similar to the one Daniel0 provided, but that verifies the anchor text isn't a link, to be ran before the pattern you're currently using. Sounds a little taxing though, so I won't even give it a try untill you confirm that's the case.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.