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