?
Solved

Converting links to text and converting text to links

Posted on 2012-09-14
8
Medium Priority
?
857 Views
Last Modified: 2013-02-18
Hello,

I have the below function that finds any text such as (http://www.google.com) in a string and convert it to link.  Also it will convert links to text.

Due to the issue ( Gridview column width which expands across the page when a long link is entered in a textbox) I need to replace the long links to a short word like (link) instead of the long link.  In other words, it takes the text and convert it to a click-able Link.  Just like creating a Hyperlink in ms office document.

The second issue I have with the below function, is when it shorten the link, I can not get it back to its original text because it replace most part of the link with dots..... This is why Shorten link function is not usable for me.

Thank you!




 private static readonly Regex regex = new Regex("((http://|https|file|www\\.)([A-Z0-9.-:]{1,})\\.[0-9A-Z?;~&#=\\-_\\./]{2,})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
       
        private static readonly string link = "<a href=\"{0}{1}\" target=_blank>{2}</a>";

        public static string ResolveLinks(string body)
        {
            if (string.IsNullOrEmpty(body))
                return body;

            foreach (Match match in regex.Matches(body))
            {
                if (!match.Value.Contains("://"))
                {
                    body = body.Replace(match.Value, string.Format(link, "http://", match.Value, ShortenUrl(match.Value, 50)));
                }
                else
                {
                  body = body.Replace(match.Value, string.Format(link, string.Empty, match.Value, ShortenUrl(match.Value, 50)));
                }
            }

            return body;
        }

        private static string ShortenUrl(string url, int max)
        {
            if (url.Length <= max)
                return url;

            // Remove the protocal
            int startIndex = url.IndexOf("://");
            if (startIndex > -1)
                url = url.Substring(startIndex + 3);

            if (url.Length <= max)
                return url;

            // Remove the folder structure
            int firstIndex = url.IndexOf("/") + 1;
            int lastIndex = url.LastIndexOf("/");
            if (firstIndex < lastIndex)
                url = url.Replace(url.Substring(firstIndex, lastIndex - firstIndex), "...");

            if (url.Length <= max)
                return url;

            // Remove URL parameters
            int queryIndex = url.IndexOf("?");
            if (queryIndex > -1)
                url = url.Substring(0, queryIndex);

            if (url.Length <= max)
                return url;

            // Remove URL fragment
            int fragmentIndex = url.IndexOf("#");
            if (fragmentIndex > -1)
                url = url.Substring(0, fragmentIndex);

            if (url.Length <= max)
                return url;

            // Shorten page
            firstIndex = url.LastIndexOf("/") + 1;
            lastIndex = url.LastIndexOf(".");
            if (lastIndex - firstIndex > 10)
            {
                string page = url.Substring(firstIndex, lastIndex - firstIndex);
                int length = url.Length - max + 3;
                url = url.Replace(page, "..." + page.Substring(length));
            }

            return url;
        }

Open in new window

0
Comment
Question by:Rad1
  • 5
  • 2
8 Comments
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 750 total points
ID: 38401109
Are you showing links inside the gridview?

Where do you call this function ShortenUrl?
0
 

Author Comment

by:Rad1
ID: 38401121
yes, the user can enter text such as www.google.com, then when user clicks on update the code will convert the text to a link.  Which then the use can just click on the www.google.com and a new google page will open.
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 750 total points
ID: 38401208
Hi Rad,
You could probably use an asp:hyperlink in your gridview item template, the navigateUrl bound to the Url the end-user entered in the gridview edit template. Expect you wouldn't need any javascript.
<asp:hyperlink id="zzz" runat="server" NavigateUrl='<%# Eval("TheUrlField") %>' Text="Go there!" target="_blank" />

Open in new window

Alan
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Rad1
ID: 38401315
Yes I agree.  But the user wants to type all their text including the hyperlink as a text, then wants to gridveiw to conver it.  This is why am doing it this way.

The main issue here is I want the code to use a generic work like a (link) instead of the long link.  This way the link Willy not expand the column too wide.
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38403507
Where do you want the original URL to be available?
0
 

Author Comment

by:Rad1
ID: 38403628
In the textbox included in the comment.
0
 

Author Comment

by:Rad1
ID: 38407869
Ok, I got it solved! But I still need some help on this issue:

It works just fine if I have one link in the string, but duplicate the first link if I have more than one link.  Any idea how I can make it works with more than one link?

Note: "Click_Link" is a hyperlink I made using another function.

Thank you!

 public string ConvLkToStr(string xstr)
        {
            string str = xstr;
            string finalStr = xstr;
            int slength = finalStr.Length;

            int strBigging = finalStr.IndexOf("http");

            if (strBigging == -1)
            {
                strBigging = finalStr.IndexOf("https");
            }
            else if (strBigging == -1)
            {
                strBigging = finalStr.IndexOf("www");
            }
            int strEnding = finalStr.IndexOf(" target=_blank");

            if (strBigging != -1 || strEnding != -1)
            {
                string recoveredLink = finalStr.Substring(strBigging, strEnding - strBigging);                
                str = str.Replace("Click_Link", recoveredLink);

                return Regex.Replace(str, "<(.|\n)*?>", string.Empty); 
            }
             else
            {
             return str;
            }
        }

Open in new window

0
 

Author Closing Comment

by:Rad1
ID: 38497919
thanks for the help!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question