3v4l.org

run code in 300+ PHP versions simultaneously
<?php $emails = [ 'a@example.com', // output: a****a@example.com 'ab@example.com', // output: a****b@example.com 'abc@example.com', // output: a****c@example.com 'abcd@example.com', // output: a****d@example.com 'abcde@example.com', // output: a****e@example.com 'abcdef@example.com', // output: a****f@example.com 'abcdefg@example.com', // output: a*****g@example.com <- 5-asterisk-fill 'Ф@example.com', // output: Ф****Ф@example.com 'ФѰ@example.com', // output: Ф****Ѱ@example.com 'ФѰД@example.com', // output: Ф****Д@example.com 'ФѰДӐӘӔӺ@example.com', // output: Ф*****Ӻ@example.com <- 5-asterisk-fill '"a@tricky@one"@example.com', // output: "************"@example.com <- multiple @ in a valid email: no problem ]; $minFill = 4; $mask = preg_replace_callback( '/^(.)(.*?)([^@]?)(?=@[^@]+$)/u', function ($m) use ($minFill) { return $m[1] . str_repeat("*", max($minFill, mb_strlen($m[2], 'UTF-8'))) . ($m[3] ?: $m[1]); }, $emails ); var_export(array_combine($emails, $mask));
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array ( 'a@example.com' => 'a****a@example.com', 'ab@example.com' => 'a****b@example.com', 'abc@example.com' => 'a****c@example.com', 'abcd@example.com' => 'a****d@example.com', 'abcde@example.com' => 'a****e@example.com', 'abcdef@example.com' => 'a****f@example.com', 'abcdefg@example.com' => 'a*****g@example.com', 'Ф@example.com' => 'Ф****Ф@example.com', 'ФѰ@example.com' => 'Ф****Ѱ@example.com', 'ФѰД@example.com' => 'Ф****Д@example.com', 'ФѰДӐӘӔӺ@example.com' => 'Ф*****Ӻ@example.com', '"a@tricky@one"@example.com' => '"************"@example.com', )

preferences:
108.81 ms | 404 KiB | 193 Q