将电子邮件和 HREFS 替换为封闭的 HREFS
我有一个电子邮件正文,以前是纯文本,但现在我已将其设为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们在这里需要一些正则表达式魔法。首先我们找到电子邮件。我希望我们不需要验证它们,因此任何不带空格的单词都带有@,后跟。没问题。
$&
- 表示正则表达式中的当前匹配。为了找到 URL,我们假设它们以某个协议名称开头,后跟
://
,同样,其中不允许有空格。这个查找 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.
$&
- 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.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.我会使用正则表达式来查找它们。看看这个博客,正则表达式在文本中查找 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.
您说您想将所有电子邮件和 URL 包含在一个字符串中 - 您的意思是引用吗?如果是这样,那么类似这样的事情就可以解决问题。它可以识别电子邮件和网址。因为我们假设字符串设置了电子邮件地址/url 的长度,所以正则表达式故意宽松 - 试图在这里过于具体可能意味着某些合法的情况不匹配。
目前尚不清楚原始文本是否包含实际的 url 编码 URL 或“可呈现的” url 解码形式。您可能希望对匹配值使用 HttpUtils.UrlEncode/UrlDecode 以确保对嵌入的 href 进行编码,同时对所显示的字符串进行解码,因此 href 包含“%20”,但这些在链接文本中显示为常规字符。
例如,如果已经存在的文本是实际的 URL,那么您可以使用
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.
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