Mask Email Address (need better solution)

Hi,
below are (working) codes to display masked email address which I'm looking for a better solution.
Current results:
john_doe@yahoo.com > displayed as > john_xxx@yahoo.com
mail.manager@gmail.com > displayed as > mail.manaxxx@gmail.com

The codes are so simple .. actually I'm looking for better solution like this:
john_doe@yahoo.com > should display as > jxxxxxxe@yxxxo.com > or better > jxxx_xxe@yxxxo.xxx
mail.manager@gmail.com > should display as > mxxxxxxxxxxr@gxxxl.com > or better > mxxx.xxxxxxr@gxxxl.xxx

can anyone help me in modifying the codes to suits what I'm looking for ... Thanks in advance.
$active = "yes"; 
 
	if (ereg("^(.+)@(.+)\\.(.+)$", $row['email'], $arr)) 
	  $total = strlen($arr['1']); 
	  $digit = ($total - 10 ); 
	
	if ( $row['mask_email'] == $active ) { $digitall = "$digit"; } 
	  else 
		{ $digitall = "3"; } 
	
	if (ereg("^(.+)@(.+)\\.(.+)$", $row['email'], $arr)) { 
	  $real = strlen($arr['1']); 
	  $sub_real = substr($arr['1'],0,$digitall); 
	  $email2 = strlen($sub_real); 
 
echo ($sub_real); 
	  while($email2 < $real) 
	  { print("x"); 
	    $email2++; 
	  } 
 
echo "@";
echo $arr['2'].".".$arr['3']; 
 
	  }

Open in new window

planetemailsAsked:
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.

yo_s_cantaCommented:
Try this;
It mask mail.manager@gmail.com to mxxx.xxxxxxr@gxxxx.xxm
<?
	$email = "mail.manager@gmail.com";
	$p = explode("@",$email);
	$name = $p[0];
	$domain = $p[1];
	//name mask
	$name1=substr($name,0,1);
	$name2=substr($name,1,-1);
	$name3=substr($name,-1,1);
	$name2 = preg_replace('/[\\w]/', 'x', $name2);
	$name=$name1.$name2.$name3;
	//domain mask
	$domain1=substr($domain,0,1);
	$domain2=substr($domain,1,-1);
	$domain3=substr($domain,-1,1);
	$domain2 = preg_replace('/[\\w]/', 'x', $domain2);
	$domain=$domain1.$domain2.$domain3;
	echo "$name@$domain";
?>

Open in new window

0
planetemailsAuthor Commented:
Ok, I'll try to adapt your codes to mine .. but having test it, yes it masks mail.manager@gmail.com to mxxx.xxxxxxr@gxxxx.xxm BUT if the data :
mail_manager@gmail.com it displays mxxxxxxxxxxr@gxxxx.xxm (no "_" displayed)

Perhaps you can modify the > $name=$name1.$name2.$name3;
so it can recognize email symbols (. and _ and -)

I'll let you know later how's the work from my side. Thanks.

PS: The emails are taken from database from users' email so there'll be a lot of email variations format.
0
Richard QuadlingSenior Software DeveloperCommented:
Outputs ...

Before : <john_doe@yahoo.com> After : <jxxn_dxe@yxxxo.cxm>
Before : <mail.manager@gmail.com> After : <mxxl.mxxxxxr@gxxxl.cxm>
Before : <a@gmail.com> After : <a@gxxxl.cxm>
Before : <ab@gmail.com> After : <ab@gxxxl.cxm>
Before : <a.b.c@gmail.com> After : <a.b.c@gxxxl.cxm>
Before : <a..b...c@gmail.com> After : <a..b...c@gxxxl.cxm>
Before : <a_bb__ccc___dddd@god_has_left_the_building.com> After : <a_bb__cxc___dxxd@gxd_hxs_lxxt_txe_bxxxxxxg.cxm>
<?php
$a_EmailAddresses = array
	(
	'john_doe@yahoo.com',
	'mail.manager@gmail.com',
	'a@gmail.com',
	'ab@gmail.com',
	'a.b.c@gmail.com',
	'a..b...c@gmail.com',
	'a_bb__ccc___dddd@god_has_left_the_building.com',
	);
 
function MaskEmail($s_Email)
	{
	// Split username and domain
	$a_EmailParts = explode('@', $s_Email);
 
	// Split each email part on any odd symbols.
	foreach($a_EmailParts as $i_EmailPart => $s_EmailPart)
		{
		// Store the split part on itself as we no longer need the 
		$a_EmailParts[$i_EmailPart] = preg_split('/[^a-z\d]/sim', $s_EmailPart);
		}
 
	// Initialize masked email address.
	$s_MaskedEmail = '';
 
	// Reconstitute the masked email address.
	foreach($a_EmailParts as $i_EmailPart => $a_EmailSubParts)
		{
		foreach($a_EmailSubParts as $i_EmailSubPart => $s_EmailSubPart)
			{
			// use the length of the sub part to determine action.
			switch(strlen($s_EmailSubPart))
				{
				case 0 :
					$s_MaskedEmail .= substr($s_Email, strlen($s_MaskedEmail), 1);
					break ;
				case 1 :
				case 2 :
					$s_MaskedEmail .= $s_EmailSubPart . substr($s_Email, strlen($s_EmailSubPart) + strlen($s_MaskedEmail), 1);
					break;
				default :
					$s_MaskedEmail .= substr($s_EmailSubPart, 0, 1) . str_repeat('x', strlen($s_EmailSubPart) - 2) . substr($s_EmailSubPart, -1) . substr($s_Email, strlen($s_EmailSubPart) + strlen($s_MaskedEmail), 1);
				}
			}
		}
 
	// Return the result.
	return $s_MaskedEmail;
	}
 
foreach($a_EmailAddresses as $s_Email)
	{
 	echo 'Before : <', $s_Email, '> After : <', MaskEmail($s_Email), '>', PHP_EOL;
	}

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Richard QuadlingSenior Software DeveloperCommented:
Doh. Of course @ is an odd symbol...

Same output of ...

Before : <john_doe@yahoo.com> After : <jxxn_dxe@yxxxo.cxm>
Before : <mail.manager@gmail.com> After : <mxxl.mxxxxxr@gxxxl.cxm>
Before : <a@gmail.com> After : <a@gxxxl.cxm>
Before : <ab@gmail.com> After : <ab@gxxxl.cxm>
Before : <a.b.c@gmail.com> After : <a.b.c@gxxxl.cxm>
Before : <a..b...c@gmail.com> After : <a..b...c@gxxxl.cxm>
Before : <a_bb__ccc___dddd@god_has_left_the_building.com> After : <a_bb__cxc___dxxd@gxd_hxs_lxxt_txe_bxxxxxxg.cxm>
<?php
$a_EmailAddresses = array
	(
	'john_doe@yahoo.com',
	'mail.manager@gmail.com',
	'a@gmail.com',
	'ab@gmail.com',
	'a.b.c@gmail.com',
	'a..b...c@gmail.com',
	'a_bb__ccc___dddd@god_has_left_the_building.com',
	);
 
function MaskEmail($s_Email)
	{
	// Split the email on any odd symbols.
	$a_EmailParts = preg_split('/[^a-z\d]/sim', $s_Email);
 
	// Initialize masked email address.
	$s_MaskedEmail = '';
 
	// Reconstitute the masked email address.
	foreach($a_EmailParts as $i_EmailPart => $s_EmailPart)
		{
		// Track the length of the part.
		$i_PartSize = strlen($s_EmailPart);
 
		// use the length of the  part to determine action.
		switch($i_PartSize)
			{
			case 0 :
				$s_MaskedEmail .= substr($s_Email, strlen($s_MaskedEmail), 1);
				break ;
			case 1 :
			case 2 :
				$s_MaskedEmail .= $s_EmailPart . substr($s_Email, $i_PartSize + strlen($s_MaskedEmail), 1);
				break;
			default :
				$s_MaskedEmail .= substr($s_EmailPart, 0, 1) . str_repeat('x', $i_PartSize - 2) . substr($s_EmailPart, -1) . substr($s_Email, $i_PartSize + strlen($s_MaskedEmail), 1);
			}
		}
 
	// Return the result.
	return $s_MaskedEmail;
	}
 
foreach($a_EmailAddresses as $s_Email)
	{
 	echo 'Before : <', $s_Email, '> After : <', MaskEmail($s_Email), '>', PHP_EOL;
	}

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
hernst42Commented:
What do you want to achiev? Protect crawlers to collect email-addresses or users from viewing the addresses at all?
0
planetemailsAuthor Commented:
@hernst42
I tried to modify a script and would like to widen its 'mask email' function as above.
So, from the admin panel. he can choose to ...  Mask Email: YES or NO
As general, I guess it's to protect users to view other user's email.

--------------------------------------------------------------------------------------------------------

@RQuadling
I think your codes need a bit modification as it's tested working only if I do this:

foreach($a_EmailAddresses as $s_Email)
        {
      echo MaskEmail($s_Email); // correct but all emails listed in one line
        //echo 'Before : <', $s_Email, '> After : <', MaskEmail($s_Email), '>', PHP_EOL;
        }

The masking function is as great as what I'm looking for .. :)
Also, to suit what I'm looking for, can you please modify the input for not arraying emails (just input for a single email) .. I mean like:
$a_EmailAddresses = john_doe@yahoo.com;
I tried as above and of course the script wont works ... :(
Also, I think better if adding 'striptolower' (I dunno where to add it) where it will convert the email to ONLY using lower case letters, rite? Please help me on this too.

Next I'll try to adapt it with my codes and hopefully I can get it working :) more guide please ...
0
planetemailsAuthor Commented:
CORRECTION:
no problem on the array, bro :) I tried it working by:
$a_EmailAddresses = array
        (
        $row['email'],
        );

.....

foreach($a_EmailAddresses as $s_Email)
        {
            echo "<b><i>";
            echo MaskEmail($s_Email);
            echo "</b></i>";
        }

So, perhaps you can help in adding the 'striptolower' and perhaps also smoothen the codes .. :) ... cos I wonder why the codes still working even without PHP_EOL;
0
Richard QuadlingSenior Software DeveloperCommented:
Only the actual function is important, the other bits are just to prove the function works.

The code is written on PHP5, so you may have to use . rather than , in the echo statement.

PHP_EOL is the way of saying "/n" or "/r/n" or "/r" depending on your OS (and may not be available in PHP4).

So, to use the function, you just supply the email.

e.g.

echo MaskEmail($s_Your_Email_Variable_Name_Goes_Here);

how ever you need to use it.

Only the function is important...
0
Richard QuadlingSenior Software DeveloperCommented:
Or even ...

$s_MaskedEmail = MaskEmail($s_UnmaskedEmail);

now you can use $s_MaskedEmail as you see fit.
0
planetemailsAuthor Commented:
hmmm ... I c .. php5 .. something that I havent really aware of .. :(

Anyway. tested working on PHP 4.4.4 .. Nice solution bro bro ...

Thanks a loT.
0
planetemailsAuthor Commented:
Thanks bro ...
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
PHP

From novice to tech pro — start learning today.