PHP 用于屏蔽多个电子邮件的正则表达式
PHP regex expresion to mask multiple email
我有一封这种格式的电子邮件。我尝试了下面的正则表达式,我无法在分号后屏蔽电子邮件。
var email = "testing.123@gmail.com;testing-test2@gmail.com";
a
//using this regex
preg_replace("/(?:(?:^|(?<=@))([^.@])|\G(?!\A))[^.@](?:([^.@])(?=[.@]))?/","*",email);
a
输出将是
t*****g.123@g***l.com;testing-test2@g***l.com
我的预期输出
t*********3@g***l.com;t***********2@g***l.com
我怎样才能做到这一点?或者有没有更有效的方法来做到这一点?谢谢
与其尝试分别匹配两种类型的子字符串(即 @
之前和 @
之后),不如考虑同时匹配两种 ,并使用 preg_replace_callback
将中间字符替换为 *
s:
$result = preg_replace_callback(
'/(?:^|(?<=;))([^@])([^@]*)([^@]@[^.])([^.]*)(?=[^.]\.)/',
function ($matches) {
return $matches[1] . str_repeat('*', strlen($matches[2])) . $matches[3] . str_repeat('*', strlen($matches[4]));
},
$str
);
https://regex101.com/r/VfS4Fh/2
模式
(?:^|(?<=;))([^@])([^@]*)([^@]@[^.])([^.]*)(?=[^.]\.)
表示:
(?:^|(?<=;))
- 从字符串的开头开始,或紧跟在 ;
之后
([^@])
- 第一组 - 捕获第一个字符
([^@]*)
- 第二组 - 捕获非 @
字符(稍后替换为 *
s)
([^@]@[^.])
- 第三组 - 捕获 @
和两边的字符
([^.]*)
- 第四组 - 捕获非 .
字符(稍后替换为 *
s)
(?=[^.]\.)
- 先行查找非 .
字符,后跟 .
然后,以相同的顺序替换为相同的组,除了第二组和第四组替换为 *
s。
我有一封这种格式的电子邮件。我尝试了下面的正则表达式,我无法在分号后屏蔽电子邮件。
var email = "testing.123@gmail.com;testing-test2@gmail.com";
a
//using this regex
preg_replace("/(?:(?:^|(?<=@))([^.@])|\G(?!\A))[^.@](?:([^.@])(?=[.@]))?/","*",email);
a
输出将是
t*****g.123@g***l.com;testing-test2@g***l.com
我的预期输出
t*********3@g***l.com;t***********2@g***l.com
我怎样才能做到这一点?或者有没有更有效的方法来做到这一点?谢谢
与其尝试分别匹配两种类型的子字符串(即 @
之前和 @
之后),不如考虑同时匹配两种 ,并使用 preg_replace_callback
将中间字符替换为 *
s:
$result = preg_replace_callback(
'/(?:^|(?<=;))([^@])([^@]*)([^@]@[^.])([^.]*)(?=[^.]\.)/',
function ($matches) {
return $matches[1] . str_repeat('*', strlen($matches[2])) . $matches[3] . str_repeat('*', strlen($matches[4]));
},
$str
);
https://regex101.com/r/VfS4Fh/2
模式
(?:^|(?<=;))([^@])([^@]*)([^@]@[^.])([^.]*)(?=[^.]\.)
表示:
(?:^|(?<=;))
- 从字符串的开头开始,或紧跟在;
之后
([^@])
- 第一组 - 捕获第一个字符([^@]*)
- 第二组 - 捕获非@
字符(稍后替换为*
s)([^@]@[^.])
- 第三组 - 捕获@
和两边的字符([^.]*)
- 第四组 - 捕获非.
字符(稍后替换为*
s)(?=[^.]\.)
- 先行查找非.
字符,后跟.
然后,以相同的顺序替换为相同的组,除了第二组和第四组替换为 *
s。