如何在PHP中捕获以下混淆的电子邮件地址? [英] How can I catch the following obfuscated email addresses in PHP?

查看:58
本文介绍了如何在PHP中捕获以下混淆的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下脚本,其中包含混淆的电子邮件地址,以及使用正则表达式模式匹配尝试使用 ***** 替换它们的功能.我的脚本尝试捕获以下单词:"at","at","at","@" ,后接一些文本(任何域名),后接"dot".""d.o.t" ,后接TLD.

Consider the following script that contains obfuscated email addresses, and a function that attempts to replace them based with ***** by using regex pattern matching. My script attempts to catch the words: "at", "a t", "a.t", "@" followed by some text (any domain name), followed by "dot" "." "d.o.t", followed by a TLD.

输入:

$str[] = 'dsfatasdfasdf asd dsfasdf dsfdsf@hotmail.com'; 
$str[] = 'I live at school where My address is dsfdsf@hotmail.com'; 
$str[] = 'I live at school. My address is dsfdsf@hotmail.com'; 
$str[] = 'at school my address is dsfdsf@hotmail.com'; 
$str[] = 'dsf a t asdfasdf asd dsfasdf dsfdsf@hotmail.com'; 
$str[] = 'd s f d s f a t h o t m a i l . c o m';

function clean_text($text){
    $pattern = '/(\ba[ \.\-_]*t\b|@)[ \.\-_]*(.+)[ \.\-_]*(d[ \.\-_]*o[ \.\-_]*t|\.)[ \.\-_]*(c[ \.\-_]*o[ \.\-_]*m|n[ \.\-_]*e[ \.\-_]*t|o[ \.\-_]*r[ \.\-_]*g|([a-z][ \.\-_]*){2,3}[a-z]?)/iU'; 
    return preg_replace($pattern, '***', $text); 
}

foreach($str as $email){ 
     echo clean_text($email); 
}

预期输出:

dsfatasdfasdf asd dsfasdf dsfdsf*** 
I live at school where My address is dsfdsf@***
I live at school. My address is dsfdsf@***
*** 
dsf *** 
d s f d s f *** 

结果:

dsfatasdfasdf asd dsfasdf dsfdsf*** 
I live *** 
I live *** 
at school my address is dsfdsf****
dsf *** 
d s f d s f *** 

问题:它捕获"at"的第一个匹配项,而不是最后一个,因此会发生以下情况:

Problem: It catches the first occurrence of "at", and not the last, so the following happens:

input: 'at school my address is dsfdsf@hotmail.com'
produces: '****'
should produce: 'at school my address is dsfdsf****'

我该如何解决?

推荐答案

基于M42的正则表达式:

Based on M42's regex:

代码:

$emails = array(
                'dsfatasdfasdf asd dsfasdf dsfdsf@hotmail.com'
                ,'I live at school where My address is dsfdsf@hotmail.com'
                ,'I live at school. My address is dsfdsf@hotmail.com'
                ,'at school my address is dsfdsf@hotmail.com'
                ,'dsf a t asdfasdf asd dsfasdf dsfdsf@hotmail.com'
                ,'d s f d s f a t h o t m a i l . c o m'
                );

foreach($emails as $email)
{
    $found = preg_match('/(.*?)((\@|a[_. -]*t)[\w .-]*?$)/', $email, $matches);
    if($found)
    {
        echo 'Username: ' . $matches[1] . ', Domain: ' . $matches[2] . "\n";
    }
}

输出:

Username: dsfatasdfasdf asd dsfasdf dsfdsf, Domain: @hotmail.com
Username: I live at school where My address is dsfdsf, Domain: @hotmail.com
Username: I live at school. My address is dsfdsf, Domain: @hotmail.com
Username: at school my address is dsfdsf, Domain: @hotmail.com
Username: dsf a t asdfasdf asd dsfasdf dsfdsf, Domain: @hotmail.com
Username: d s f d s f , Domain: a t h o t m a i l . c o m

这篇关于如何在PHP中捕获以下混淆的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆