C Programming Tutorials and advice
CProgrammingTrends News Archives About Us Feedback

Recent Articles

Yahoo SiteExplorer API With C#
If you are interested in using Yahoo's SiteExplorer API and are coding in the ASP.NET environment using C# (web pages) then the following method might be of use to you. This method is used for returning the number of...

Debug JavaScript In Visual Studio 2005 Like C#
I've been using Visual Studio 2005 for almost 2½ years since the beta 1 release. In all that time, I've used Firefox and Firebug for all JavaScript debugging. I've tried setting breakpoints in JavaScript in Visual Studio before...

From VB.NET To C#
Coming from the Visual Basic world, I used to find many examples throughout the web written in VB and VB.NET. Then I got hired by Traceworks which was a VB.NET company, but they decided to move to C# because they couldn't find employees that wanted to code VB.NET.

Bind Countries From CultureInfo Class In C#
Some people have asked me how BlogEngine.NET displays a dropdown list of countries when no source XML file is present. The simple answer is that you don't need any external list to bind to from C#, you can instead...

See Who Calls Any Method In C#
In some situations it can be important to know which methods are calling other methods. Think of an error log. Here you would like to know which method that threw the error and at what line in what file. This helps to...

Compress & Decompress Strings In C#
A couple of days ago, I had to store some very big strings in an XML file. To keep the file size down I wanted to compress the strings using a GZipStream and then decompress them later when needed. I modified...



Recent WebProNews Articles

Senate Prepares Grill For Google
Google's proposed purchase of DoubleClick has drawn the scrutiny of privacy advocates, the Federal Trade Commission, and now a Senate Judiciary subcommittee. Google will find out if the $580,000 spent on...

Shop.org Summit: Dangers Of Keyword Research
Looking for great keyword choices for your ad campaign? What you've found so far could be damaged goods. WebProNews has been covering the Shop.org Summit, taking place in Las Vegas. You can catch our...

Go Go Google Gadget Ads
A new interactive ad format has entered the world of widgets with Google's release of Gadget Ads, touted as 'Websites within Websites'. AdWords has a new toy for its advertising clients. They are called Google Gadget...

Google AdSense For Mobile Arrives
There’s no such thing as a portable La-Z-Boy, but there are comfortable camping chairs. And although a 42-inch LCD might be ideal, pocket-sized video devices are also popular. Now, with the formal launch of AdSense...

Google Presents PowerPoint Alternative
The rumored arrival of a Google option for creating and viewing slide-based presentations became fact with the debut of its newest feature on Google Docs. You'll see something different when clicking New in Google...

CBS Just Can't Get Along with Bloggers?
It was bloggers who forced CBS Evening News anchor Dan Rather into early retirement, and yet CBS – at least somebody there – is still being condescending towards the new media. This story began in pursuit of an...



09.20.07


Resolve & Shorten URLs In C#


By Mads Kristensen

Recently I've needed a method that would look at some text and automatically discover all URLs and turn them into hyperlinks.

I've done that before so it was a matter of copy/paste. This time it was a little more complicated, because the resolved URLs could not be longer than 50 characters long. That was important because otherwise it would break the design. A long URL doesn't word wrap so it would end up bleeding out of the design.

So, the challenge was to resolve the URLs and turn them into links, while keeping the anchor text at a max of 50 characters long. To shorten the URL is easy enough, but it all comes down to how you want it shortened.

The rules

1. If the URL is longer than 50 characters then remove "http://".

2. If it still is longer than allowed it must compress the folder structure like shown below.

http://www.microsoft.com/windows/server/2003/compare.aspx -> http://www.microsoft.com/.../compare.aspx

3. If the URL is still longer, then it must look for query strings and fragments and remove them as well.

Download Now!

The code







Implementation

To use these methods, just call the ResolveLinks method like so:

string body = ResolveLinks(txtComment.Text);

It works on URLs with or without the http:// protocol prefix. In other words http://www.example.com/ and http://www.example.com/ resolves to the same URL. This technique is implemented in the comments on this blog. You can test it by writing a comment with a URL in it.

Comments

About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

About CProgrammingTrends
A collection of articles and tutorials designed to help C and C variant programmers in their programming work. C Programming Tutorials and advice

CProgrammingTrends is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagmentNews.com


-- CProgrammingTrends is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2007 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal


archives | advertising info | news headlines | free newsletters | comments/feedback | submit article




CProgrammingTrends Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact