i defined ToHtml() extension for string class and convert break to <br/>.
how can detect hyper link and convert to <a> element?
public static class StringExtension
{
public static string ToHtml(this string item)
{
//\r\n windows
//\n unix
//\r mac os
return item.Replace("\r\n", "<br/>").Replace("\n", "<br/>").Replace("\r", "<br/>");
}
}
c#, asp.net
see this one, uses regex
private string ConvertUrlsToLinks(string msg) {
string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
return r.Replace(msg, "<a href=\"$1\" title=\"Click to open in a new window or tab\" target=\"_blank\">$1</a>").Replace("href=\"www", "href=\"http://www");
}
You could use a regular expression here to identify where the hyperlink begins and ends (possibly based on the length of the match) Then add in your anchor tags before and after.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With