Forum Moderators: coopster & phranque

Message Too Old, No Replies

Regular Expression help

I need to strip all but links

         

Bonusbana

11:28 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



Hi

I need a regular expression that strips all data from a string, except the <a> tags and what they contain.

I knwo that this will detect the links:
"/<a([^>]+)>([^<]+)</a>/","<a\\1>\\2</a>"

But how do I strip out everything else through a regExp?

thanks

timster

1:59 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's two changes that need to happen with the new expression: first, you turn the order around.

Second, you need to add ^ and $ to match the beginning and end of the string respectively, to get any text before and after the first and last links.

Adapting the code you provided, it'd look something like this:

s/(^¦\<\/a>).*?(\<a([^>]+)>([^<]+)¦$)/$1$2/g;

In PHP, just cause I did that...

$string = $string = preg_replace("/^¦(\<\/a>).*?(\<a([^>]+)>([^<]+))¦$/","\\1\\2", $string);