2013-12-12 15 views
7

私は単純な友達/バディシステムを構築しています。誰かが新しい友達を検索しようとすると、そのユーザーが誰であるかを知るために、実際の詳細を明らかにすることはありません。PHPで電子メールアドレスを部分的に非表示にする

[email protected]abcdl******@hotmail.comになります。私が書いたテストとして

<?php 
$email = "[email protected]"; 

$em = explode("@",$email); 
$name = $em[0]; 
$len = strlen($name); 
$showLen = floor($len/2); 
$str_arr = str_split($name); 
for($ii=$showLen;$ii<$len;$ii++){ 
    $str_arr[$ii] = '*'; 
} 
$em[0] = implode('',$str_arr); 
$new_name = implode('@',$em); 
echo $new_name; 

これは動作しますが、同じロジックを適用するのいずれかより簡単/短い道があった場合、私は不思議でしたか?正規表現のように?例えば

+0

しかし、どのような場合、ユーザーの実際の電子メールアドレスがちょうど 'のABCの@ example.com'のですか?あなたはそれからすべてを明らかにしました。ユーザー名を使った自動提示は大丈夫ですが、電子メールアドレスの自動提示はプライバシーの悪夢のようです。 –

+1

@MichaelBerkowskiそのため、$ showLen = floor($ len/2)を使用しているので、電子メールの約半分がいつも隠されています。 – Bluemagica

答えて

17

がここに迅速な何か:

function obfuscate_email($email) 
{ 
    $em = explode("@",$email); 
    $name = implode(array_slice($em, 0, count($em)-1), '@'); 
    $len = floor(strlen($name)/2); 

    return substr($name,0, $len) . str_repeat('*', $len) . "@" . end($em); 
} 

// to see in action: 
$emails = ['"Abc\@def"@iana.org', '[email protected]']; 

foreach ($emails as $email) 
{ 
    echo obfuscate_email($email) . "\n"; 
} 

のエコー:

"Abc\*****@iana.org 
abcdl*****@hotmail.com 

substr()str_repeat()

+0

これは私が探していたものです。実際に 'floor($ len/2)'が必要ですが、それがなければ、奇数の文字を使用すると実際の文字の数より1小さいアスタリスクが得られます。 – Bluemagica

+0

これは素晴らしいことですが、アスタリスクの数を正確にするために追加しますが、2つの異なる$ len値を許可する必要があります。上記のように '$ len'を計算し(' floor'を含む)、 '$ len2 = strlen($ name) - $ len'を計算します。最後に、文字列を 'return substr($ name、0、$ len) 'として返します。 str_repeat( '*'、$ len2)。 "@"。 end($ em) ' –

+0

' 1 @ xyz.com'のような電子メールでは動作しません。 '@ xyz.com'だけを返します。理想的には '* @ xyz 'を返すべきです。com' –

2

:あなたのような何か与える

substr($email, 0, 3).'****'.substr($email, strpos($email, "@")); 

abc****@hotmail.com

+0

これはなぜ-2になったのか、ちょっと混乱しました。私はこれがdownvoteをするためにdownvoterがコメントを残す必要があるべきである理由だと思う。私が知る限り、これはOPが求めたこととまったく同じです。 – Jdahern

+0

シンプル&ワーキング+1 – eljuko

+0

「[email protected]」はどのようなものにマスキングされるのでしょうか。 – garkin

0

を使用していますが、この機能を試してみてください。これは有効なメール("Abc\@def"@iana.orgなど)で動作します。

function hideEmail($email){ 
    $prefix = substr($email, 0, strrpos($email, '@')); 
    $suffix = substr($email, strripos($email, '@')); 
    $len = floor(strlen($prefix)/2); 

    return substr($prefix, 0, $len) . str_repeat('*', $len) . $suffix; 
} 

echo hideEmail('[email protected]'); 
echo hideEmail('"abc\@def"@iana.org'); 

戻り

abcdljtrs*********@hotmail.com 
"abc\*****@iana.org 
3

ここでは、このための私の代替ソリューションです。

電子メールの元の長さに一致するマスク文字の正確な数は使用しませんが、プライバシー上の理由から固定長のマスクを使用します。また、最大許容文字を表示し、メールの半分以上を表示しないように設定します。私はまた、すべての電子メールを最小の長さ以下にマスクします。

function maskEmail($email, $minLength = 3, $maxLength = 10, $mask = "***") { 
    $atPos = strrpos($email, "@"); 
    $name = substr($email, 0, $atPos); 
    $len = strlen($name); 
    $domain = substr($email, $atPos); 

    if (($len/2) < $maxLength) $maxLength = ($len/2); 

    $shortenedEmail = (($len > $minLength) ? substr($name, 0, $maxLength) : ""); 
    return "{$shortenedEmail}{$mask}{$domain}"; 
} 

テスト:

$email = ""; 
$tests = []; 
for ($i=0; $i < 22; $i++) { 
    $email .= chr(97 + $i); 

    $tests[] = $email . " -> " . maskEmail("{$email}@example.com"); 
} 
print_r($tests); 

結果:

Array 
(
    [0] => a -> ***@example.com 
    [1] => ab -> ***@example.com 
    [2] => abc -> ***@example.com 
    [3] => abcd -> ab***@example.com 
    [4] => abcde -> ab***@example.com 
    [5] => abcdef -> abc***@example.com 
    [6] => abcdefg -> abc***@example.com 
    [7] => abcdefgh -> abcd***@example.com 
    [8] => abcdefghi -> abcd***@example.com 
    [9] => abcdefghij -> abcde***@example.com 
    [10] => abcdefghijk -> abcde***@example.com 
    [11] => abcdefghijkl -> abcdef***@example.com 
    [12] => abcdefghijklm -> abcdef***@example.com 
    [13] => abcdefghijklmn -> abcdefg***@example.com 
    [14] => abcdefghijklmno -> abcdefg***@example.com 
    [15] => abcdefghijklmnop -> abcdefgh***@example.com 
    [16] => abcdefghijklmnopq -> abcdefgh***@example.com 
    [17] => abcdefghijklmnopqr -> abcdefghi***@example.com 
    [18] => abcdefghijklmnopqrs -> abcdefghi***@example.com 
    [19] => abcdefghijklmnopqrst -> abcdefghij***@example.com 
    [20] => abcdefghijklmnopqrstu -> abcdefghij***@example.com 
    [21] => abcdefghijklmnopqrstuv -> abcdefghij***@example.com 
) 
3

私はこれを使用しています:

念頭に置いて、それらのルールで

は、ここでは、オプションのパラメータを持つ私の機能です

function secret_mail($email) 
{ 

$prop=2; 
    $domain = substr(strrchr($email, "@"), 1); 
    $mailname=str_replace($domain,'',$email); 
    $name_l=strlen($mailname); 
    $domain_l=strlen($domain); 
     for($i=0;$i<=$name_l/$prop-1;$i++) 
     { 
     $start.='x'; 
     } 

     for($i=0;$i<=$domain_l/$prop-1;$i++) 
     { 
     $end.='x'; 
     } 

    return substr_replace($mailname, $start, 2, $name_l/$prop).substr_replace($domain, $end, 2, $domain_l/$prop); 
} 

好きになるでしょう出力何か:あまりに最後の文字を表示することが時々その良い cyxxxxxone @ gmxxxxcom

1

ABCDEFZ @ gmail。comは になります。A*****[email protected]

私はあなたのことを簡単にしておくことをお勧めします。 たぶん、このようなものは、@記号

function mask_email($email) { 
    /* 
    Author: Fed 
    Simple way of masking emails 
    */ 

    $char_shown = 3; 

    $mail_parts = explode("@", $email); 
    $username = $mail_parts[0]; 
    $len = strlen($username); 

    if($len <= $char_shown){ 
     return implode("@", $mail_parts); 
    } 

    //Logic: show asterisk in middle, but also show the last character before @ 
    $mail_parts[0] = substr($username, 0 , $char_shown) 
     . str_repeat("*", $len - $char_shown - 1) 
     . substr($username, $len - $char_shown + 2 , 1 ) 
     ; 

    return implode("@", $mail_parts); 
} 
0

前に、私は機能

function hide_email($email){ 
$final_str = ''; 
$string = explode('@', $email); 
$leftlength = strlen($string[0]); 
$string2 = explode('.', $string[1]); 
$string2len = strlen($string2[0]); 
$leftlength_new = $leftlength-1; 
$first_letter = substr($string[0], 0,1); 
$stars = ''; 
$stars2 = ''; 
for ($i=0; $i < $leftlength_new; $i++) { 
    $stars .= '*'; 
} 
for ($i=0; $i < $string2len; $i++) { 
    $stars2 .= '*'; 
} 
$stars; 
return $final_str .= $first_letter.$stars.'@'.$stars2.'.'.$string2[1]; 

}を持って十分な https://github.com/fedmich/PHP_Codes/blob/master/mask_email.php

マスク最初の3つの文字を表示するために電子メールをして、最後の文字に簡単です

echo hide_email('[email protected] ');

+0

あなたのコードに関する情報を教えてもらえますか? – jhhoff02

+0

このコードは、最初の文字の後に文字を星に置き換えます。また、メールドメインを星に置き換えてください。 例:[email protected]はH****@***.comになります –

0

@の前に1文字がある場合に問題が発生しました。私は以下の機能に修正しました。

function obfuscate_email($email) 

{ 

    $em = explode("@",$email); 
    if(strlen($em[0])==1){ 
     return '*'.'@'.$em[1]; 
    } 
    $name = implode(array_slice($em, 0, count($em)-1), '@'); 
    $len = floor(strlen($name)/2); 
    return substr($name,0, $len) . str_repeat('*', $len) . "@" . end($em); 

} 
2

私は機能が誰かを助けることができる作成

function hideEmail($email) 
{ 
    $mail_parts = explode("@", $email); 
    $length = strlen($mail_parts[0]); 
    $show = floor($length/2); 
    $hide = $length - $show; 
    $replace = str_repeat("*", $hide); 

    return substr_replace ($mail_parts[0] , $replace , $show, $hide) . "@" . substr_replace($mail_parts[1], "**", 0, 2); 
} 

hideEmail("[email protected]"); // output: na**@**ample.com 
hideEmail("[email protected]"); // output: some*****@**ample.com 

あなたが好きなあなたがカスタマイズすることができます..このようなもの

function hideEmail($email) { 
    $mail_parts = explode("@", $email); 
    $length = strlen($mail_parts[0]); 

    if($length <= 4 & $length > 1) 
    { 
     $show = 1; 
     }else{ 
     $show = floor($length/2);  
    } 

    $hide = $length - $show; 
    $replace = str_repeat("*", $hide); 

    return substr_replace ($mail_parts[0] , $replace , $show, $hide) . "@" . substr_replace($mail_parts[1], "**", 0, 2); 
} 

hideEmail("[email protected]"); // output: n***@**ample.com 
hideEmail("[email protected]"); // output: some*****@**ample.com 
1
(長さは最初の4以下の表示の場合)

これはあなたが望むものではないかもしれませんが、私はこのために行くでしょう:

<?php 

    /* 

    Here's the logic: 

    We want to show X numbers. 
    If length of STR is less than X, hide all. 
    Else replace the rest with *. 

    */ 

function mask($str, $first, $last) { 
    $len = strlen($str); 
    $toShow = $first + $last; 
    return substr($str, 0, $len <= $toShow ? 0 : $first).str_repeat("*", $len - ($len <= $toShow ? 0 : $toShow)).substr($str, $len - $last, $len <= $toShow ? 0 : $last); 
} 

function mask_email($email) { 
    $mail_parts = explode("@", $email); 
    $domain_parts = explode('.', $mail_parts[1]); 

    $mail_parts[0] = mask($mail_parts[0], 2, 1); // show first 2 letters and last 1 letter 
    $domain_parts[0] = mask($domain_parts[0], 2, 1); // same here 
    $mail_parts[1] = implode('.', $domain_parts); 

    return implode("@", $mail_parts); 
} 

$emails = array(
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]', 
    '[email protected]' 
); 

foreach ($emails as $email){ 
    echo '<b>'.$email.'</b><br>'.mask_email($email).'<br><hr>'; 
} 

結果:

[email protected] 
*@*.com 

[email protected] 
**@**.com 

[email protected] 
***@***.com 

[email protected] 
ab*[email protected]*a.com 

[email protected] 
ab**[email protected]**a.com 

[email protected] 
ab***[email protected]***a.com 

[email protected] 
ab****[email protected]****a.com 

[email protected] 
ab*****[email protected]*****a.com 

[email protected] 
ab******[email protected]******a.com 
0

これは古いスレッド&ですが、すでに多くの答えを持っています。自分のスニペットも共有したい

有効なメールかどうかを確認します。 &を検閲する文字数を指定します。 どのキャラクターが検閲に使用されるべきですか?

function get_censored_email($email, $show_chars = 3, $censor_char = '*') { 
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 

    $char_length = strlen($email); 
    $censor_count = $char_length - $show_chars; 

    $return_email = substr($email, 0, $show_chars); 
    $return_email .= str_repeat("*", $censor_count); 

    return $return_email; 
} 

}

$email = '[email protected]'; 
echo get_censored_email($email, 3, '*'); // returns nom*********************** 
関連する問題