正则表达式检测普通电子邮件和 mailto: 链接电子邮件

发布于 2024-10-18 13:38:24 字数 1446 浏览 5 评论 0 原文

我的用户使用 CMS 来输入工作机会。在这些工作机会中,有时电子邮件地址采用纯格式(请联系[email  ;protected])或作为 html mailto: 链接 ([电子邮件受保护]">jobline 以及更烦人的 [电子邮件受保护]">[电子邮件受保护])。

我想构建一个 php 函数,它可以找到任何一种格式,并通过构建告诉人们该做什么的 html 字符串来使它们防垃圾邮件,并通过 javascript 为启用 javascript 的设置重建正确的可点击 mailto:link 。这是我遇到问题的检测部分。

以下内容非常适合普通电子邮件。我如何调整它来检测 mailto: 链接?

$addr_pattern = '/([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})(\((.+?)\))?/i';
preg_match_all($addr_pattern, $content, $addresses);
$the_addrs = $addresses[0];
for ($a = 0; $a < count($the_addrs); $a++) {
     $repaddr[$a] = preg_replace($addr_pattern, '<span title="$5" class="pep-email">$1(' . $opt_val . ')$2.$3</span>', $the_addrs[$a]);
 }
 $cc = str_replace($the_addrs, $repaddr, $content);

PS:这是为了改进现有的 WordPress 插件:Pixeline 的电子邮件保护器。获胜答案的作者将在插件代码、描述和变更日志中得到记述。

My users use a CMS to enter job offers. In these job offers, sometimes the email address is in plain format (please contact [email protected]) or as an html mailto: link (<a href="mailto:[email protected]">jobline</a> and the even more annoying one <a href="mailto:[email protected]">[email protected]</a>).

I would like to build a php function that finds either format and make them spamproof by building an html string that tells humans what to do, and via javascript reconstruct a proper clickable mailto:link for javascript-enabled setups. It's the detection part that i have problem with.

The following works perfect for plain email. How can i adapt it to detect mailto: links too?

$addr_pattern = '/([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})(\((.+?)\))?/i';
preg_match_all($addr_pattern, $content, $addresses);
$the_addrs = $addresses[0];
for ($a = 0; $a < count($the_addrs); $a++) {
     $repaddr[$a] = preg_replace($addr_pattern, '<span title="$5" class="pep-email">$1(' . $opt_val . ')$2.$3</span>', $the_addrs[$a]);
 }
 $cc = str_replace($the_addrs, $repaddr, $content);

PS: this is to improve an existing wordpress plugin: Pixeline's Email protector. Winning answer's author will be dully credited in the plugin code, description and changelog.

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

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

发布评论

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

评论(2

杀手六號 2024-10-25 13:38:24

最好使用 domdocument 类来获取实际链接,因为有很多不同的可能方法来编写它们。您还可以将其与正则表达式一起使用来扫描整个内容以同时替换文本。

    // The content
$content = 'The stuff from the page';

// Start the dom object
$dom = new DOMDocument();
$dom->recover = true;
$dom->substituteEntities = true;

// Feed the content to the dom object
$dom->loadHTML($content);

// Check each link
foreach ($dom->getElementsByTagName('a') as $anchor) {
// Get the href
$href = $anchor->getAttribute('href');
// Check if it's a mailto link
if (substr($href, 0, 7) == 'mailto:') {
    # Do something with it
    $href = 'new link href';
}
// Put it back in the link
$anchor->setAttribute('href', $href);
}

// Replace the content with the new content
$content = $dom->saveHTML();

It would be better to use the domdocument class to get the actual links as there are so many different possible ways to write them. You can also use it with a regex to scan the entire content to replace the text at the same time.

    // The content
$content = 'The stuff from the page';

// Start the dom object
$dom = new DOMDocument();
$dom->recover = true;
$dom->substituteEntities = true;

// Feed the content to the dom object
$dom->loadHTML($content);

// Check each link
foreach ($dom->getElementsByTagName('a') as $anchor) {
// Get the href
$href = $anchor->getAttribute('href');
// Check if it's a mailto link
if (substr($href, 0, 7) == 'mailto:') {
    # Do something with it
    $href = 'new link href';
}
// Put it back in the link
$anchor->setAttribute('href', $href);
}

// Replace the content with the new content
$content = $dom->saveHTML();
嘿看小鸭子会跑 2024-10-25 13:38:24
(<a href="mailto:|)([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})(">.+?</a>|)

这应该匹配所有变体,然后替换为 $2

(<a href="mailto:|)([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})(">.+?</a>|)

This should match all variations then replace with $2

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