我的用户使用 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.
发布评论
评论(2)
最好使用 domdocument 类来获取实际链接,因为有很多不同的可能方法来编写它们。您还可以将其与正则表达式一起使用来扫描整个内容以同时替换文本。
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.
这应该匹配所有变体,然后替换为 $2
This should match all variations then replace with $2