regular expression. stripping anchor tag

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

regular expression. stripping anchor tag

Post by alvinphp »

Anyone know how to strip the anchor tag information so that it only shows the display name? I have two examples below along with what I want it to be...

<a href='test.php?id=5A'>red</a> -> red
<a href='test.php?id=1B1'>blue</a> -> blue

I could probably figure it out if I was given 5 hours, but I am hoping someone here knows. :D
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

$text = preg_replace('/<a[^<>]*?>(.+?)<\/a>/i', "$1", $text);
However, that can be easily exploited by nesting anchors.

This should be in the Regex forum.
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post by alvinphp »

Thanks Jen, just what I needed. And yes, I should of put this in the regex forum. :?

I haven't worked with regex in over a year, it is all coming back to me now. Just need a little push start! I just created 4 more regex's.
Post Reply