Page 1 of 1

Stripping <A HREF tags

Posted: Tue Feb 28, 2006 6:09 pm
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

Posted: Tue Feb 28, 2006 6:13 pm
by feyd
I think you want \\2 instead of \\3

Posted: Tue Feb 28, 2006 6:18 pm
by John Cartwright
Remember, the matches start at 0 not 1

Re: Stripping <A HREF tags

Posted: Fri Mar 03, 2006 11:35 pm
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.