将电子邮件地址中的用户名部分替换为星号 [英] Replace username part in email addresses into asterisks

查看:44
本文介绍了将电子邮件地址中的用户名部分替换为星号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将电子邮件地址中的用户名转换为星号.用户名中的第一个和最后一个字母保持原样,其余字母替换为 (*).

How can I convert username in email addresses into asterisks. The first and last letter in the username stay as it is and rest replaced with (*).

示例:

mhyunf@gmail.com

进入

m****f@gmail.com

推荐答案

您可以使用环顾四周来完成.

You can do it using look arounds.

/(?!^).(?=[^@]+@)/

  • (?!^) 消极的看后面.检查字符前面是否没有字符串开头.这可确保未选择第一个字符.

    • (?!^) Negative look behind. Checks if the character is not preceded by start of string. This ensures that the first character is not selected.

      . 匹配单个字符.

      (?=[^@]+@) 积极展望.确保匹配的单个字符后跟除 @(由 [^@] 确保)和 @

      (?=[^@]+@) Positive look ahead. Ensures that the single character matched is followed by anything other than @ ( ensured by [^@] ) and then a @

      正则表达式演示

      示例

      preg_replace("/(?!^).(?=[^@]+@)/", "*", "mhyunf@gmail.com")
      =>  m****f@gmail.com
      

      这篇关于将电子邮件地址中的用户名部分替换为星号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆