Forum Moderators: coopster

Message Too Old, No Replies

Stripping Out Hyperlinks

         

MrSpeed

4:04 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am trying to strip out hypelinks in a string.

Is there a simple function or class to remove hyperlinks but retain the link text or image?

I considered stripping out all html with a regex but I'd like to leave tables, images etc.

I just want to delink hyperlinks.

EDIT:
It looks like I could replace "href" with something like "xhref" to create invalid html, which will work in my case

mooger35

4:21 pm on Mar 29, 2006 (gmt 0)

10+ Year Member



you could also just replace "href" with "".

end result <a ='link.php'>Link</a>. Link comes out as plain text.

coopster

4:31 pm on Mar 29, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I like mooger35's idea. Using preg_replace() [php.net] to strip out the href attributes. Untested:
$pattern = "/href=['\"\s]?[^\s>]*[\s>]/i"; 
$string = preg_replace($pattern, '', $string);

Says to find any 'href=' followed by an optional opening single quotation mark, double quotation mark, or whitespace entity character followed by zero or more of anything that isn't a whitespace entity character or the end of the opening <a> element (the greater than sign part of it), followed by either a whitespace entity or the greater than sign, which is the end of the opening <a element.