将电子邮件和 HREFS 替换为封闭的 HREFS

发布于 2024-09-03 04:09:47 字数 448 浏览 6 评论 0原文

我有一个电子邮件正文,以前是纯文本,但现在我已将其设为 HTML。电子邮件是使用多种方法生成的,但没有一种方法可以轻松转换。

我所要做的是:

Some content [email protected], some http://www.somewebsite/someurl.aspx.

我想做的是创建一个函数,自动将所有电子邮件地址和所有 URL 包含在 HREF 标记中的 字符串 内,以便 HTML 电子邮件在所有电子邮件客户端中都能正确读取。

有人有这个功能吗?

I have an Email body that used to be plain 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 [email protected], 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有伤那来痛 2024-09-10 04:09:47

我们在这里需要一些正则表达式魔法。首先我们找到电子邮件。我希望我们不需要验证它们,因此任何不带空格的单词都带有@,后跟。没问题。

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:
amp;\">
amp;</a>" );
}

$& - 表示正则表达式中的当前匹配。

为了找到 URL,我们假设它们以某个协议名称开头,后跟 ://,同样,其中不允许有空格。

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=\"
amp;\">
amp;</a>" );
}

这个查找 ftp、http 或 https 链接,但您可以将任何协议添加到正则表达式中,并用 | (管道)分隔它,如下所示: (file|telnet|ftp|http(s) ?)://[^\s]*)

实际上URL中也可能有@http://username:password@host:port/,但我希望情况并非如此,因为这样我们就必须使用一些更严格的正则表达式。

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:
amp;\">
amp;</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=\"
amp;\">
amp;</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.

我ぃ本無心為│何有愛 2024-09-10 04:09:47

我会使用正则表达式来查找它们。看看这个博客,正则表达式在文本中查找 URL 并将其作为链接 是一个很好的起点。

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.

黑色毁心梦 2024-09-10 04:09:47

您说您想将所有电子邮件和 URL 包含在一个字符串中 - 您的意思是引用吗?如果是这样,那么类似这样的事情就可以解决问题。它可以识别电子邮件和网址。因为我们假设字符串设置了电子邮件地址/url 的长度,所以正则表达式故意宽松 - 试图在这里过于具体可能意味着某些合法的情况不匹配。

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;
}

目前尚不清楚原始文本是否包含实际的 url 编码 URL 或“可呈现的” url 解码形式。您可能希望对匹配值使用 HttpUtils.UrlEncode/UrlDecode 以确保对嵌入的 href 进行编码,同时对所显示的字符串进行解码,因此 href 包含“%20”,但这些在链接文本中显示为常规字符。

例如,如果已经存在的文本是实际的 URL,那么您可以使用

    email = email.Replace(match.Value, "<a href=" + match.Value + ">" + 
       HttpUtils.UrlEncode(match.Value.Substring(1,match.Value.Length-2)) + "</a>");

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>");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文