I have an Email body that used to be pla开发者_运维百科in text, but now I've made it HTML. The emails are generated using a number of methods and none of them are easy to convert.
What I have is:
Some content emailaddress@something.com, some http://www.somewebsite/someurl.aspx.
What I'd like to do is create a function that automatically encloses all email addresses and all URLs within a string in HREF tags so that the HTML email reads properly in all email clients.
Does anyone have a function for this?
We need here some regular expression magic. First we find the emails. I hope we need not to validate them, so any word without spaces with @ followed by . is ok.
public static string MakeEmailsClickable( string input ){
if (string.IsNullOrEmpty(input) ) return input;
Regex emailFinder = new Regex(@"[^\s]+@[^\s\.]+.[^\s]+", RegexOptions.IgnoreCase);
return emailFinder.Replace(input, "<a href=\"mailto:$&\">$&</a>" );
}
$&
- represents the current match in regex.
To find Urls we asume that they start with some protocol name followed by ://
, again no spaces are allowed in it.
public static string MakeUrlsClickable( string input ){
if (string.IsNullOrEmpty(input) ) return input;
Regex urlFinder = new Regex(@"(ftp|http(s)?)://[^\s]*", RegexOptions.IgnoreCase);
return urlFinder.Replace(input, "<a href=\"$&\">$&</a>" );
}
This one looks for ftp, http or https links, but you can add any protocol into regex separating it with |
(pipe) like that: (file|telnet|ftp|http(s)?)://[^\s]*)
.
Actually URL may also have @ in it http://username:password@host:port/
, but I hope this is not the case, as then we will have to use some more strict regular expressions.
I would use a regular expression to find them. Take a look at this blog, Regex to find URL within text and make them as link a good starting point.
You say you want to enclose all email and URLs that are within a string - you mean quoted? If so, then something like this will do the trick. It recognises emails, and urls. Because we are assuming the string sets the length of the email address/url, then the regexes are deliberately lax - trying to be too specific here may mean that some ligitimate cases are not matched.
public string LinkQuotedEmailsAndURLs(string email)
{
Regex toMatch = new Regex("((https?|ftp)?://([\\w+?\\.\\w+])+[^ \"]*)|\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*", RegexOptions.IgnoreCase);
MatchCollection mactches = toMatch.Matches(email);
foreach (Match match in mactches) {
email = email.Replace(match.Value, "<a href=" + match.Value + ">" + match.Value.Substring(1,match.Value.Length-2) + "</a>");
}
return email;
}
It's not clear if the original text contain the actual url-encoded URLs or "presentable" url-decoded forms. You may want to use HttpUtils.UrlEncode/UrlDecode on the match value to ensure that the embedded href is encoded, while the presented string is decoded, so the href includes "%20" but these are shown as regular characters in the link text.
E.g. if the text already present is the actual URL, then you can use
email = email.Replace(match.Value, "<a href=" + match.Value + ">" +
HttpUtils.UrlEncode(match.Value.Substring(1,match.Value.Length-2)) + "</a>");
精彩评论