2018-12-23 Pujan Niroula 1 Minute(s) Read

How To Partially Hide Email Address in PHP

img-partially hide email with php.jpg

Perhaps you may noticed twitter, facebook, google partially hide email address during forgot password process. To reduce risk of user enumeration vulnerability. If you want to do same in your PHP web application then this post is for you.

twitter email partially hidden screenshot

In this tutorial I will show step by step method to hide email address partially. It will display just few character of email address and email domain.

Hide Email Address Partially in PHP

Lets do it, First we will verify email is valid or not then we replace each except first two character with '*'.

<?php
function hide_email($email)
{
    if(filter_var($email, FILTER_VALIDATE_EMAIL))
    {
        list($first, $last) = explode('@', $email);
        $first = str_replace(substr($first, '2'), str_repeat('*', strlen($first)-2), $first);
        $last = explode('.', $last);
        $last_domain = str_replace(substr($last['0'], '1'), str_repeat('*', strlen($last['0'])-1), $last['0']);
        $hide_email = $first.'@'.$last_domain.'.'.$last['1'];
        return $hide_email;
    }
}
$email = "contact@gmail.com";
echo hide_email($email); //co*****@g****.com
?>

At first we validate email with PHP's pre-loaded function filter_var and FILTER_VALIDATE_EMAIL

After that I broke email into array with explode function. The first part of email is before '@' sign and second part of email is after '@' sign. Strlen() function returns length of string.

Str_replace() will replace first parameter with second parameter in third parameter.

Substr() will remove all character after given second parameter in the string. Similarly str_repeat() repeats the given string.

Finally I collected all strings on $hide_email and return it.

Hope you like this tutorial, I will be happy to see you in comment section below.

Comments (6) Add New Comment
demo pg soft mirip asli2024-09-27 11:06 PM

Hello, I do think your blog might be having web browser compatibility problems. Whenever I look at your blog in Safari, it looks fine however, when opening in IE, it has some overlapping issues. I simply wanted to provide you with a quick heads up! Aside from that, fantastic site!

Reply
macau2024-09-24 7:28 AM

My programmer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using WordPress on numerous websites for about a year and am nervous about switching to another platform. I have heard very good things about blogengine.net. Is there a way I can transfer all my wordpress posts into it? Any kind of help would be greatly appreciated!

Reply
macauslot2024-09-22 7:31 PM

Excellent way of describing, and nice piece of writing to take facts about my presentation subject matter, which i am going to deliver in university.

Reply
slot gacor2024-09-22 12:28 PM

I am in fact thankful to the owner of this web page who has shared this enormous piece of writing at at this place.

Reply
macauslot2024-09-20 6:21 AM

Hey There. I found your blog using msn. This is an extremely well written article. I will make sure to bookmark it and come back to read more of your useful info. Thanks for the post. I'll definitely return.

Reply
macauslot2024-09-16 10:30 AM

Everyone loves it whenever people come together and share ideas. Great blog, stick with it!

Reply