Automatically bit.ly-ing URLS in a text block

Gosh, what a long title. I don’t like writing blog post titles, but that one seems to make sense.

I wrote this code the other day as a kind of demo for an idea at work.

We allow some HTML in forum posts at work, but if a user puts in loads of long URLs then they hit the post limit quickly. So we wanted to write some code that would scan through a post, find the URLs, shorten them, and then cap the post to it’s limit.

Instead of using a bit.ly API here (other URL shortening services are available) I’ve just reversed the href, so http://site.com becomes moc.etis.//:ptth… which was quite a lot of fun in itself.

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.

arrHref = reMatchNoCase(']*>',longText);

writeDump(arrHref);

cntHref = arrayLen(arrHref);

for(h = 1; h <= cntHref; h++) {
thisHref = arrHref[h];
thisHref = reReplaceNoCase(thisHref,'
','1');
reverseHref = reverse(thisHref);

longText = replaceNoCase(longText,thisHref,reverseHref);
}

#longText#


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