正则表达式部分隐藏电子邮件? [英] regexp to partly hide email?

查看:43
本文介绍了正则表达式部分隐藏电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的目标很简单,我想要诸如

It's rather simple what I'm trying to achieve, I want input such as

漢aelena@tratata.com

成为:

漢******@tratata.com

所以我制作了这个正则表达式来匹配第一个字符和@".

So I made this regexp to match between the first char and the '@'.

mb_regex_encoding ('UTF-8' );
mb_ereg_replace('(?<=^.{1}).*?(?=@)','*','漢aelena@tratata.com',1);

问题是,它只会匹配一次,因此只会在那里放一颗星,而不是六颗.像这样的东西,就是我会得到的:

The problem though, it would only match it one time, and thus would only put in there one star, instead of six. Something like this, is what I would get:

漢*@tratata.com

然后我想使用mb_ereg_replace_callback,返回:

Then I wanted to use mb_ereg_replace_callback, to return:

return $matches[1].str_repeat('*', strlen($matches[1]));

然后我阅读了规范,它说 mb_ereg_replace_callback 在 PHP 5.4.1 或更高版本中可用.

Then I read specs and it said mb_ereg_replace_callback is available in PHP 5.4.1 or later.

...有什么想法可以实现同样的目标吗?

...Any ideas how could I achieve the same thing?

推荐答案

不需要使用回调函数,一个正则表达式即可.

There is no need to use a callback function, a single regular expression can do it.

(?<=.).(?=.*@)

  • (?<=.),请确保前面至少有一个字符,以免替换第一个字符.
  • .,匹配任意字符.
  • (?=.*@),确保字符后面有一个@.
    • (?<=.), make sure there is at least one character before so it won't replace the first character.
    • ., match any character.
    • (?=.*@), make sure there is a @ somewhere after the character.
    • 将函数更改为带有 unicode 修饰符的 preg_replace 示例(如建议):

      Example with function changed to preg_replace with unicode modifier (as suggested):

      echo preg_replace('/(?<=.).(?=.*@)/u','*','漢aelena@tratata.com');
      

      输出:

      漢******@tratata.com
      

      这篇关于正则表达式部分隐藏电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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