PHP obfuscated or hide part of the email address

I wrote my own function for one of the project to display first 3 characters and last characters before the @. In between the first and last 3 characters, put in 3 star (*).

So, this is how it looks like


/**
* hide part of the email address
* @param string data, email address
* @return string obfuscated email address
*/
public static function process_obfuscated_email($data)
{
	//obfuscate email address
	$expl_email = explode("@", $data);
	$email_name = $expl_email['0'];
	$length = strlen($email_name);
	
	if($length >=9)
	{
		$first_3 = substr($email_name, 0, 3);
		$last_3 = substr($email_name, -3);
		$obfuscated_email = $first_3."***".$last_3."@".$expl_email['1'];
	}
	if($length < 9)
	{
		$av_sub = $length - 6;
		if( $av_sub > 0 )
		{
			$first_3 = substr($email_name, 0, 3);
			$last = substr($email_name, -$av_sub);
			$obfuscated_email = $first_3."***".$last."@".$expl_email['1'];
		}
		elseif( $av_sub == 0 )
		{
			$first_3 = substr($email_name, 0, 2);
			$last = substr($email_name, -2);
			$obfuscated_email = $first_3."***".$last."@".$expl_email['1'];
		}
		else
		{
			$first = substr($email_name, 0, 1);
			$last = substr($email_name, -1);
			$obfuscated_email = $first."***".$last."@".$expl_email['1'];
		}
	}
	return $obfuscated_email;
}

By Keenlio, January 17, 2014

2 Comments

  1. Bettie says:

    I read a lot of interesting content here. Probably you spend a lot of time writing, i know how to save
    you a lot of work, there is an online tool that creates high quality, google friendly posts in seconds, just search in google – laranitas free
    content source

  2. Frann Leach says:

    Thanks. This is just what I was looking for.

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *


8 − three =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>