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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.