代码之家  ›  专栏  ›  技术社区  ›  Mike Sherov

如何在PHP中捕获以下模糊的电子邮件地址?

  •  0
  • Mike Sherov  · 技术社区  · 14 年前

    ***** 使用正则表达式模式匹配。我的剧本试图抓住: "at", "a t", "a.t", "@" 后跟一些文本(任何域名),后跟 "dot" "." "d.o.t" ,然后是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 *** 
    

    问题:

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

    我怎样才能解决这个问题?

    3 回复  |  直到 14 年前
        1
  •  2
  •   Daniel R    14 年前

    基于M42的正则表达式:

    $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
    
        2
  •  1
  •   Toto    14 年前

    这是一个Perl脚本,能适应php吗?

    my @l = (
    '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(@l) {
       s/(\@|a[_. -]*t)[\w .-]*?$/****/;
       print $_,"\n";
    }
    

    dsfatasdfasdf asd dsfasdf dsfdsf****
    I live at school where My address is dsfdsf****
    I live at school. My address is dsfdsf****
    at school my address is dsfdsf****
    dsf a t asdfasdf asd dsfasdf dsfdsf****
    d s f d s f ****
    
        3
  •  0
  •   cynicaljoy    14 年前
    function clean_text($text){
        $pattern = '/\w+[\w-\.]*(\@\w+((-\w+)|(\w*))\.[a-z]{2,3})/i';
        preg_match($pattern, $text, $matches);
    
        return (isset($matches[1])) ? str_replace($matches[1], "****", $text) : $text;
    }
    

    唯一一个不匹配的是你的最后一个,但你明白了。