In this post, I will show you “How to Partially hide email address in PHP”.
I will tell you how it could be possible using simple PHP code. We can hide any email address as like any******@g***.com.
As you noticed on facebook, google, twitter etc, where they are doing same practice which I am going to tell you. When you clicked forgot your password then they show you only partially hide email address to verify you. It’s your email address or not.
I will show you in two examples. We will create a simple PHP function for it and you can simply call that function with an argument where we will pass email address which needs to be converted. Normally, we create functions for reusability. So let’s begin to create functions:
Example 1:
<?php // Example 1 function hideEmailAddress($email) { if(filter_var($email, FILTER_VALIDATE_EMAIL)) { list($first, $last) = explode('@', $email); $first = str_replace(substr($first, '3'), str_repeat('*', strlen($first)-3), $first); $last = explode('.', $last); $last_domain = str_replace(substr($last['0'], '1'), str_repeat('*', strlen($last['0'])-1), $last['0']); $hideEmailAddress = $first.'@'.$last_domain.'.'.$last['1']; return $hideEmailAddress; } } ?>
Output:
any************@g****.com
Example 2:
<?php // Example 2 function hideEmailAddress($email) { $em = explode("@",$email); $name = implode(array_slice($em, 0, count($em)-1), '@'); $len = floor(strlen($name)/2); return substr($name,0, $len) . str_repeat('*', $len) . "@" . end($em); } $email = 'anyone@gmail.com'; echo hideEmailAddress($email); ?>
Output:
anyo*******@gmail.com
I hope it will helps you a-lot to figure out “How to Partially hide email address in PHP?”. If you found this article useful then strongly share this post with others/friends by clicking on share button.