Forum Moderators: phranque

Message Too Old, No Replies

Rewrite To Remove Links

         

carfac

5:05 pm on May 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe not the best forum to post this, but at least here I know Jim (The KING of rewrites!) will see it.

I have a field in my database that occasionally has links in it. I post a shortened version of this on a main page, with a link to "more information". Anyway, these links appear at different points, and are a plague to me. I would like to run a substitution to remove them... but (obviously) the links vary, and I am not sure if I can use pattern matching.

This is what I would like to do:

$desc =~ s/<a href=\"(.*)\">//g;

Can I do this, or something like it?

Thanks!

Dave

jdMorgan

6:00 pm on May 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but two complicating factors:

1) You'll also need some logic to remove "</a>" if "a href" is removed.
-and-
2) You may occasionally see things like <a title="my spammy link" href="example.com"> or <a style="none" href="http://example.com" title="my spammy link">

so you need another ".*" - type subpattern both before and after "href=".*".

And as always, I suggest that you NOT use ".*" in any case, as it can easily lead to unexpected results. Use a negative-match like ' [^">]+\" ' instead. (Including the '>' allows the negative-match pattern to match even malformed links, where the '>' occurs without the closing quote.

This regex is rather complex, and I couldn't come up with it in the time I have right now... But you can probably work through it before I get free. :)

Jim

carfac

1:01 am on May 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim!

Yes, I was conscious of removing the "</a>"... but I can do that easily, and figured not to muddy the waters with that- but thanks for the reminder.

The field I am doing this replace on is all data I have entered- not user contributed stuff- so no worries about non-conformity... they are all straight "<a href..." links!

This is a "description" field in my database- and I want just 100 or so characters to be on the main page, with a "More Here" link to a full description page.

Thanks!

Dave