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
Stripping <A HREF tags
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Stripping <A HREF tags
If you have /i then you don't need [aA], that is redundant.castonzo wrote:Hey All,
return preg_replace('/<[aA][^<]*href=["|\']?([^ "\']*)["|\']?[^>].*>([^<]*)</a>/i',' \\3', $string);
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>/iAlso note the escaping of the / in the </a> tag.