using php: create a regex or str_replace or preg_replace to hide a portion of an email

given an email

show first 3 letters/numbers

and replace the next letters numbers with
*
until the
@

bobjones@gmail.com
bob*****@gmail.com

amyadams@yahoo.com
amy*****@yahoo.com

richardsmith@aol.com
ric*********@aol.com
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Maybe something like this (untested)

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// HIDE PART OF EMAIL ADDRESS
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28262950.html

$e = 'bobjones@gmail.com';

function mute($eml, $len=3)
{
    $pre = substring($eml,0,$len);
    $arr = explode('@', $eml);
    $arr[0] = $pre . '***';
    return implode('@', $arr);
}

echo "<br>$e";
echo mute($e);

Open in new window

0
Terry WoodsIT GuruCommented:
If you'd like a regular expression:

print preg_replace('#(?<=.{3}).(?=.*@)#','*',$email);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Tested and works.  

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// HIDE PART OF EMAIL ADDRESS
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28262950.html

$e = 'bobjones@gmail.com';

function mute($eml, $len=3)
{
    $pre = substr($eml,0,$len);
    $arr = explode('@', $eml);
    $arr[0] = $pre . '***';
    return implode('@', $arr);
}

echo "<br>$e";
echo '<br>' . mute($e);

Open in new window

But as with most code examples, the quality of your answer will depend on the quality of your test data!
0
rgb192Author Commented:
thanks for
regex
substr, explode, implode
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.