0

*****難読化された電子メール アドレスを含む次のスクリプトと、正規表現パターン マッチングを使用してそれらを置き換えようとする関数について考えてみましょう。私のスクリプトは、次の単語をキャッチしようとします:"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 *** 

問題: 「at」の最後の出現ではなく、最初の出現をキャッチするため、次のようになります。

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

どうすればこれを修正できますか?

4

3 に答える 3

2

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
于 2010-07-27T19:22:19.733 に答える
1

これは 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 ****
于 2010-07-27T18:52:02.907 に答える
0
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;
}

これが一致しないのは最後のものだけですが、要点はわかります。

于 2010-07-27T16:28:26.660 に答える