-1

In my website, I want to give a feature "to recognize the link". You can see the example in Facebook. Whenever we share a link, it automatically detects it and also find thumbnails. Also If we do the same in Status update, It does the same.

I want to implement the same functionality using asp.net with c#.

Any tutorials, links or demos will be helpful. If you can, please share the logic too.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
RTRokzzz
  • 235
  • 1
  • 4
  • 14

1 Answers1

3

This is what i use. On description I use 40 characters, if you want the complete link in description too just remove the substring part...

static string LinksToHTML(string str)
    {
         Regex urlRx = new Regex(@"(?<url>((mailto\:|(news|(ht|f)tp(s?))\://){1}\S+))", RegexOptions.IgnoreCase);

        MatchCollection matches = urlRx.Matches(str);

        foreach (Match match in matches)
        {
            var url = match.Groups["url"].Value;
            str = str.Replace(url, string.Format("<a href=\"{0}\" target=\"blank\">{1}</a>", url, (url.Length > 40 ? url.Substring(0, 40) + "..." : url)));

            //str = str.Replace(url, string.Format("<a href=\"{0}\" target=\"blank\">{1}</a>", url, url));
        }

        return str;
    }
erichste
  • 759
  • 4
  • 16