Stripping <A HREF tags

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

Moderator: General Moderators

Post Reply
castonzo
Forum Newbie
Posts: 1
Joined: Tue Feb 28, 2006 6:06 pm
Location: Chicago

Stripping <A HREF tags

Post by castonzo »

Hey All,

I'm trying to parse HTML documents and remove all the hyperlinks while keeping the linked text. Here's what I'm trying:

function stripHref($string)
{
return preg_replace('/<[aA][^<]*href=["|\']?([^ "\']*)["|\']?[^>].*>([^<]*)</a>/i',' \\3', $string);
}

It's returning nothing but I cannot figure out why.

Any help would be great.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think you want \\2 instead of \\3
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Remember, the matches start at 0 not 1
User avatar
NacreData
Forum Newbie
Posts: 2
Joined: Thu Feb 23, 2006 10:10 am

Re: Stripping <A HREF tags

Post by NacreData »

castonzo wrote:Hey All,


return preg_replace('/<[aA][^<]*href=["|\']?([^ "\']*)["|\']?[^>].*>([^<]*)</a>/i',' \\3', $string);
If you have /i then you don't need [aA], that is redundant.

Are you being specific about href because you want to leave anchor tags but remove links?

Why match and capture the href attribute if you are not using it? I think you are not using it, am I correct? If you want to capture links but not anchors wouldn't

Code: Select all

/<a[^>]*href[^>]*>([^<]*)<\/a>/i
work?

Also note the escaping of the / in the </a> tag.
Post Reply