Email Hide Preview Function

Written by @phuang 4 November 2022

This handy function hides an email in the following format for preview purposes k*****@g********

Code Snippet:

                                                
                                                <!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
<?php

function formatEmailForPreview($email){

    $domain = str_split(strstr($email, "@"));
    array_shift($domain);
    $user = str_split(strstr($email, "@", true));

    $user_format = "";
    for($i = 0; $i < count($user); $i++){
        if($i == 0 || $i == (count($user) - 1)){
            $user_format .=  $user[$i];
        }else{
            $user_format .= "*";
        }

    }

    $domain_format = "";

    for($i = 0; $i < count($domain); $i++){
        if($i == 0 || $i == (count($domain) - 1)){
            $domain_format .=  $domain[$i];
        }else{
            $domain_format .= "*";
        }

    }

    return $user_format . "@" . $domain_format;

}

?><a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @phuang

Help desk analyst transitioning to a full-stack developer role. Hoping to learn and collaborate with everyone. Happy coding!

P

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2