Editing all the links in a page

I’m not sure if this is a duplicate post of not… I couldn’t find the original one, so here goes anyway.

We had the situation at work where we wanted to find all the links in a news article, in the back end editing system, and send them all to something like bit.ly to shorten them, so make it less likely that the article hit the maximum word count.

This might have been a forum post actually, anyway, that’s besides the point.

So I came up with a quick example to work out how to do it. Instead of using a URL shortening API for the demo I’ve just reversed the hrefs. But it would be easy to swap them that code out for bit.ly or something similar.

Edit HREF's demo

This is totally copied from Ben Nadel's blog, an idea which he got in turn from A Book Apart: Mobile First, but I thought it was worth mentioning again.


#longText#

// find the a tags
arrHref = reMatchNoCase(']*>',longText);

// get the array length and loop over
cntHref = arrayLen(arrHref);
for(h = 1; h LTE cntHref; h++) {

// get the individual href,
thisHref = arrHref[h];
thisHref = reReplaceNoCase(thisHref,'
','1');

// reverse it, bit.ly it, whatever takes you're fancy
reverseHref = reverse(thisHref);

// replace the original href with the new one
longText = replaceNoCase(longText,thisHref,reverseHref);
}



#longText#

You can see the live sample here: http://skeater.co.uk/samples/hrefEdit.cfm

It might not be completely robust, it might need updating for production use but the concept is there and it works.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s