Replacing characters in email address with *

Replacing characters in email address with *
<?php
$email = 'youremail@ddre.ss';

$beforeAt=explode("@",$email);
$beforeAt=$beforeAt[0];
$asterisks=str_repeat("*",strlen($beforeAt)-2);
$beforeAt=$beforeAt{0}.$asterisks.substr($beforeAt,-1);
$domain=explode("@",$email);
$domain=explode(".",$domain[1]);
$extension=$domain[count($domain)-1];
$domain=implode(".",$domain);
$domain=substr($domain,0,-1*strlen($extension)-1);
$asterisks=str_repeat("*",strlen($domain)-1);
$email=$beforeAt."@".substr($domain,0,1).$asterisks.".".$extension;

echo $email;
?>