任何人帮助我用简单的方法解决这个问题...我有这个邮件地址abcedf@gmail.com 。
如何转换此邮件地址a****f@gmail.com
我尝试使用strpos并获取@但我无法获得中间值并将其更改为**** 。 任何人都有助于找到这个问题。
Anyone Help me to solve this in simple method... I have this mail address abcedf@gmail.com.
How to convert this mail address a****f@gmail.com
I tried using strpos and get @ but I cannot get middle values and change it to ****. Anyone help to find this problem.
最满意答案
对于这个问题,应该实现一些重要的考虑因素,但我先提供一种方法,然后通过这项任务深入研究一些潜在的危害......
(通常认为最好的做法是使用非正则表达式方法,而不是太复杂。这将使用非正则表达式方法。)
代码:( 演示 )
// only processing valid emails, so at least one character before @ $emails=['a@gmail.com', // output: a****a@gmail.com 'ab@gmail.com', // output: a****b@gmail.com 'abc@gmail.com', // output: a****c@gmail.com 'abcd@gmail.com', // output: a****d@gmail.com 'abcde@gmail.com', // output: a****e@gmail.com 'abcdef@gmail.com', // output: a****f@gmail.com 'abcdefg@gmail.com' // output: a*****g@gmail.com <- 5-asterisk fill ]; $fill=4; // minimum number of asterisks to inject foreach($emails as $email){ $user=strstr($email,'@',true); // extract entire substring before @ $len=strlen($user); // measure length of username string if($len>$fill+2){$fill=$len-2;} // increase fill amount for more asterisks in str_repeat $starred=substr($user,0,1).str_repeat("*",$fill).substr($user,-1).strstr($email,'@'); // 1st char-----------^^^ fill--------^^^^^^^^^ last char-----^^ ^^^^^^^^^^^^^^^^^-the rest of the original email string echo "$starred\n"; }输出:
a****a@gmail.com a****b@gmail.com a****c@gmail.com a****d@gmail.com a****e@gmail.com a****f@gmail.com a*****g@gmail.com上述方法在最小有效子字符串长度上不会失败; 假设所有输入值都是有效的电子邮件。
该方法通过在“mailbox”子字符串中间插入至少4个星号,为每封电子邮件添加身份保护。 要增加或减少星号数,只需修改$fill 。 (将$fill值保持在最小值1 )
当在电子邮件中多次出现子字符串时,此页面上当前发布的所有答案都可以避免多次替换,例如: abc@abc.abc我只想提及这一考虑因素,因为错误将意味着电子邮件地址将获得损坏并可能揭示用户实际电子邮件地址的线索。
不幸的是,此页面上的其他两个答案无法适应其方法中短邮箱子串的可能性。 这导致电子邮件地址完全暴露和/或出现通知......
//Pedro Lobito: // $email="a@gmail.com"; output: a@gmail.com <-- not okay // $email="ab@gmail.com"; output: ab@gmail.com <-- not okay // $email="abc@gmail.com"; output: a*c@gmail.com <-- okay //Sahil Gulati: // $string="a@gmail.com"; output: NOTICE x2 and a@gmail.com <-- not okay // $string="ab@gmail.com"; output: NOTICE x2 and ab@gmail.com <-- not okay // $string="abc@gmail.com"; output: a*c@gmail.com <-- okay对于OP的用例,其他海报的方法漏洞可能永远不会成为问题 - 也许是因为电子邮件列表是公司的员工电子邮件列表,公司的电子邮件地址格式确保所有邮箱子串都是大于两个字符。 但是,以规范的方式回答这个问题将为希望实现类似方法的所有用户带来更大的价值。
There are a few significant considerations that should be realized for this question, but I'll provide a method first, then delve deeper into some potential hazards with this task...
(It is generally considered best practice to use non-regex methods when it is not too convoluted to do so. This will use a non-regex approach.)
Code: (Demo)
// only processing valid emails, so at least one character before @ $emails=['a@gmail.com', // output: a****a@gmail.com 'ab@gmail.com', // output: a****b@gmail.com 'abc@gmail.com', // output: a****c@gmail.com 'abcd@gmail.com', // output: a****d@gmail.com 'abcde@gmail.com', // output: a****e@gmail.com 'abcdef@gmail.com', // output: a****f@gmail.com 'abcdefg@gmail.com' // output: a*****g@gmail.com <- 5-asterisk fill ]; $fill=4; // minimum number of asterisks to inject foreach($emails as $email){ $user=strstr($email,'@',true); // extract entire substring before @ $len=strlen($user); // measure length of username string if($len>$fill+2){$fill=$len-2;} // increase fill amount for more asterisks in str_repeat $starred=substr($user,0,1).str_repeat("*",$fill).substr($user,-1).strstr($email,'@'); // 1st char-----------^^^ fill--------^^^^^^^^^ last char-----^^ ^^^^^^^^^^^^^^^^^-the rest of the original email string echo "$starred\n"; }Output:
a****a@gmail.com a****b@gmail.com a****c@gmail.com a****d@gmail.com a****e@gmail.com a****f@gmail.com a*****g@gmail.comThe above method will not fail on minimum valid substring length; it is assumed that all input values will be valid emails.
The method adds identity protection to each email by inserting a minimum of 4 asterisks in the middle of "mailbox" substring. To increase or decrease the number of asterisks just modify $fill. (Keep $fill value at a minimum value of 1.)
All currently posted answers on this page do well to avoid multiple replacements when a substring occurs more than once in the email, for instance: abc@abc.abc I only wanted to mention this consideration because getting it wrong would mean the email address would get mangled and possibly reveal clues about the user's actual email address.
Unfortunately the other two answers on this page fail to accommodate for the possibility of short mailbox substrings in their methods. This results in email addresses being fully exposed and/or a NOTICE appearing...
//Pedro Lobito: // $email="a@gmail.com"; output: a@gmail.com <-- not okay // $email="ab@gmail.com"; output: ab@gmail.com <-- not okay // $email="abc@gmail.com"; output: a*c@gmail.com <-- okay //Sahil Gulati: // $string="a@gmail.com"; output: NOTICE x2 and a@gmail.com <-- not okay // $string="ab@gmail.com"; output: NOTICE x2 and ab@gmail.com <-- not okay // $string="abc@gmail.com"; output: a*c@gmail.com <-- okayFor the OP's use case, it is possible that the other posters' method vulnerabilities will never be an issue -- perhaps because the list of emails is a company's employee email list and the company has an email address format that ensures that all mailbox substrings are larger than two characters. However, answering this question in a canonical way will generate a greater value to all users that wish to implement a similar method.
隐藏带星星的电子邮件地址(*)(Hide email address with stars (*))任何人帮助我用简单的方法解决这个问题...我有这个邮件地址abcedf@gmail.com 。
如何转换此邮件地址a****f@gmail.com
我尝试使用strpos并获取@但我无法获得中间值并将其更改为**** 。 任何人都有助于找到这个问题。
Anyone Help me to solve this in simple method... I have this mail address abcedf@gmail.com.
How to convert this mail address a****f@gmail.com
I tried using strpos and get @ but I cannot get middle values and change it to ****. Anyone help to find this problem.
最满意答案
对于这个问题,应该实现一些重要的考虑因素,但我先提供一种方法,然后通过这项任务深入研究一些潜在的危害......
(通常认为最好的做法是使用非正则表达式方法,而不是太复杂。这将使用非正则表达式方法。)
代码:( 演示 )
// only processing valid emails, so at least one character before @ $emails=['a@gmail.com', // output: a****a@gmail.com 'ab@gmail.com', // output: a****b@gmail.com 'abc@gmail.com', // output: a****c@gmail.com 'abcd@gmail.com', // output: a****d@gmail.com 'abcde@gmail.com', // output: a****e@gmail.com 'abcdef@gmail.com', // output: a****f@gmail.com 'abcdefg@gmail.com' // output: a*****g@gmail.com <- 5-asterisk fill ]; $fill=4; // minimum number of asterisks to inject foreach($emails as $email){ $user=strstr($email,'@',true); // extract entire substring before @ $len=strlen($user); // measure length of username string if($len>$fill+2){$fill=$len-2;} // increase fill amount for more asterisks in str_repeat $starred=substr($user,0,1).str_repeat("*",$fill).substr($user,-1).strstr($email,'@'); // 1st char-----------^^^ fill--------^^^^^^^^^ last char-----^^ ^^^^^^^^^^^^^^^^^-the rest of the original email string echo "$starred\n"; }输出:
a****a@gmail.com a****b@gmail.com a****c@gmail.com a****d@gmail.com a****e@gmail.com a****f@gmail.com a*****g@gmail.com上述方法在最小有效子字符串长度上不会失败; 假设所有输入值都是有效的电子邮件。
该方法通过在“mailbox”子字符串中间插入至少4个星号,为每封电子邮件添加身份保护。 要增加或减少星号数,只需修改$fill 。 (将$fill值保持在最小值1 )
当在电子邮件中多次出现子字符串时,此页面上当前发布的所有答案都可以避免多次替换,例如: abc@abc.abc我只想提及这一考虑因素,因为错误将意味着电子邮件地址将获得损坏并可能揭示用户实际电子邮件地址的线索。
不幸的是,此页面上的其他两个答案无法适应其方法中短邮箱子串的可能性。 这导致电子邮件地址完全暴露和/或出现通知......
//Pedro Lobito: // $email="a@gmail.com"; output: a@gmail.com <-- not okay // $email="ab@gmail.com"; output: ab@gmail.com <-- not okay // $email="abc@gmail.com"; output: a*c@gmail.com <-- okay //Sahil Gulati: // $string="a@gmail.com"; output: NOTICE x2 and a@gmail.com <-- not okay // $string="ab@gmail.com"; output: NOTICE x2 and ab@gmail.com <-- not okay // $string="abc@gmail.com"; output: a*c@gmail.com <-- okay对于OP的用例,其他海报的方法漏洞可能永远不会成为问题 - 也许是因为电子邮件列表是公司的员工电子邮件列表,公司的电子邮件地址格式确保所有邮箱子串都是大于两个字符。 但是,以规范的方式回答这个问题将为希望实现类似方法的所有用户带来更大的价值。
There are a few significant considerations that should be realized for this question, but I'll provide a method first, then delve deeper into some potential hazards with this task...
(It is generally considered best practice to use non-regex methods when it is not too convoluted to do so. This will use a non-regex approach.)
Code: (Demo)
// only processing valid emails, so at least one character before @ $emails=['a@gmail.com', // output: a****a@gmail.com 'ab@gmail.com', // output: a****b@gmail.com 'abc@gmail.com', // output: a****c@gmail.com 'abcd@gmail.com', // output: a****d@gmail.com 'abcde@gmail.com', // output: a****e@gmail.com 'abcdef@gmail.com', // output: a****f@gmail.com 'abcdefg@gmail.com' // output: a*****g@gmail.com <- 5-asterisk fill ]; $fill=4; // minimum number of asterisks to inject foreach($emails as $email){ $user=strstr($email,'@',true); // extract entire substring before @ $len=strlen($user); // measure length of username string if($len>$fill+2){$fill=$len-2;} // increase fill amount for more asterisks in str_repeat $starred=substr($user,0,1).str_repeat("*",$fill).substr($user,-1).strstr($email,'@'); // 1st char-----------^^^ fill--------^^^^^^^^^ last char-----^^ ^^^^^^^^^^^^^^^^^-the rest of the original email string echo "$starred\n"; }Output:
a****a@gmail.com a****b@gmail.com a****c@gmail.com a****d@gmail.com a****e@gmail.com a****f@gmail.com a*****g@gmail.comThe above method will not fail on minimum valid substring length; it is assumed that all input values will be valid emails.
The method adds identity protection to each email by inserting a minimum of 4 asterisks in the middle of "mailbox" substring. To increase or decrease the number of asterisks just modify $fill. (Keep $fill value at a minimum value of 1.)
All currently posted answers on this page do well to avoid multiple replacements when a substring occurs more than once in the email, for instance: abc@abc.abc I only wanted to mention this consideration because getting it wrong would mean the email address would get mangled and possibly reveal clues about the user's actual email address.
Unfortunately the other two answers on this page fail to accommodate for the possibility of short mailbox substrings in their methods. This results in email addresses being fully exposed and/or a NOTICE appearing...
//Pedro Lobito: // $email="a@gmail.com"; output: a@gmail.com <-- not okay // $email="ab@gmail.com"; output: ab@gmail.com <-- not okay // $email="abc@gmail.com"; output: a*c@gmail.com <-- okay //Sahil Gulati: // $string="a@gmail.com"; output: NOTICE x2 and a@gmail.com <-- not okay // $string="ab@gmail.com"; output: NOTICE x2 and ab@gmail.com <-- not okay // $string="abc@gmail.com"; output: a*c@gmail.com <-- okayFor the OP's use case, it is possible that the other posters' method vulnerabilities will never be an issue -- perhaps because the list of emails is a company's employee email list and the company has an email address format that ensures that all mailbox substrings are larger than two characters. However, answering this question in a canonical way will generate a greater value to all users that wish to implement a similar method.
发布评论