PHP How To Partially Hide or Mask Email Addresses Tutorial

Reading Time: 3 minutes
836 Views

Inside this article we will see the concept i.e PHP How To Partially Hide or Mask Email Addresses Tutorial. Article contains the classified information about How to hide email address partially in PHP.

If you are looking for a solution i.e Hint or partially hide email address with stars (*) in PHP then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Read More: How To Use Limit and Offset in CodeIgniter 4 Query

Examples

onl**************@g****.com

learn********@gmail.com

onlinewe********@gmail.com 

Let’s get started.

Application Programming

Create a folder say mask-email in your localhost directory. Create a file index.php into it.

Method #1

<?php

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 = 'onlinewebtutorhub@gmail.com';

echo hideEmailAddress($email);

Output

onlinewe********@gmail.com

Method #2

<?php

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;
    }
}

$email = 'onlinewebtutorhub@gmail.com';

echo hideEmailAddress($email);

Output

onl**************@g****.com

We hope this article helped you to learn PHP How To Partially Hide or Mask Email Addresses Tutorial in a very detailed way.

Read More: CodeIgniter 4 How To Get Domain Name From Subdomain

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more