pufferz Posted October 24, 2009 Share Posted October 24, 2009 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 Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 24, 2009 Share Posted October 24, 2009 Something like this should suffice: $str = 'foo <a href="foo bar" title="test">bar</a> baz'; echo preg_replace('#<a.*?>([^>]*)</a>#i', '$1', $str); Quote Link to comment Share on other sites More sharing options...
cags Posted October 24, 2009 Share Posted October 24, 2009 Just out of interest, why did you (Daniel0) choose [^>] as the pattern match for the anchor text? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 24, 2009 Share Posted October 24, 2009 That was a mistake. It was supposed to have been [^<], i.e. this instead: $str = 'foo <a href="foo bar" title="test">bar</a> baz'; echo preg_replace('#<a.*?>([^<]*)</a>#i', '$1', $str); Quote Link to comment Share on other sites More sharing options...
salathe Posted October 24, 2009 Share Posted October 24, 2009 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? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 24, 2009 Share Posted October 24, 2009 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? Quote Link to comment Share on other sites More sharing options...
pufferz Posted October 24, 2009 Author Share Posted October 24, 2009 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? Quote Link to comment Share on other sites More sharing options...
cags Posted October 24, 2009 Share Posted October 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.