Hint or partially hide email address with stars (*) in PHP

Hint or partially hide email address with stars (*) in PHP

To partially hide an email address with stars (*) in PHP, you can split the email address into parts and replace some characters with stars. Here's how you can do it:

function partiallyHideEmail($email) { // Split email address into local part and domain part list($localPart, $domainPart) = explode('@', $email); // Determine the number of characters to hide in the local part $hiddenCharacters = max(strlen($localPart) - 4, 0); // Replace characters in the local part with stars $hiddenLocalPart = substr_replace($localPart, str_repeat('*', $hiddenCharacters), 2, $hiddenCharacters); // Return partially hidden email address return $hiddenLocalPart . '@' . $domainPart; } // Example usage $email = 'example@example.com'; $partiallyHiddenEmail = partiallyHideEmail($email); echo $partiallyHiddenEmail; // Output: ex********@example.com 

In this example:

  • The partiallyHideEmail function takes an email address as input.
  • It splits the email address into the local part and the domain part using explode.
  • It determines the number of characters to hide in the local part. We keep at least 4 characters visible to ensure the email remains recognizable.
  • It replaces characters in the local part with stars (*) using substr_replace.
  • It then returns the partially hidden email address.

You can adjust the number of visible characters and the placement of stars according to your requirements.

Examples

  1. PHP: Partially hide email address

    • Description: Replace characters in the email address with asterisks (*) except for the first letter before the '@' symbol.
    $email = "example@example.com"; $hiddenEmail = substr($email, 0, 1) . str_repeat('*', strpos($email, '@') - 1) . substr($email, strpos($email, '@')); echo $hiddenEmail; 

    Explanation: This code snippet hides characters in the email address except for the first character and the domain part.

  2. PHP: Hide part of email address with stars

    • Description: Replace characters in the local part of the email address with asterisks (*).
    $email = "example@example.com"; list($local, $domain) = explode('@', $email); $hiddenLocal = str_repeat('*', strlen($local)) . '@' . $domain; echo $hiddenLocal; 

    Explanation: This code snippet hides the local part of the email address while keeping the domain part visible.

  3. PHP: Mask email address

    • Description: Mask the email address completely except for the domain part.
    $email = "example@example.com"; list($local, $domain) = explode('@', $email); $maskedEmail = str_repeat('*', strlen($local)) . '@' . $domain; echo $maskedEmail; 

    Explanation: This code snippet replaces the local part of the email address with asterisks (*) while displaying the domain part.

  4. PHP: Secure display of email address

    • Description: Securely display the email address with hidden characters.
    function hideEmail($email) { $parts = explode('@', $email); $local = $parts[0]; $domain = $parts[1]; $maskedLocal = str_repeat('*', strlen($local)) . '@' . $domain; return $maskedLocal; } $email = "example@example.com"; echo hideEmail($email); 

    Explanation: This function hides the local part of the email address with asterisks (*) and returns the modified email.

  5. PHP: Mask email address with substr

    • Description: Use substr function to mask part of the email address.
    $email = "example@example.com"; $maskedEmail = substr($email, 0, strpos($email, '@') - 1) . str_repeat('*', strlen(substr($email, strpos($email, '@')))); echo $maskedEmail; 

    Explanation: This code snippet uses substr function to hide the local part of the email address and keeps the domain part visible.

  6. PHP: Hide email address with str_replace

    • Description: Use str_replace function to mask the local part of the email address.
    $email = "example@example.com"; $maskedLocal = str_replace(substr($email, 0, strpos($email, '@')), str_repeat('*', strpos($email, '@')), $email); echo $maskedLocal; 

    Explanation: This code snippet uses str_replace function to hide the local part of the email address and keeps the domain part visible.

  7. PHP: Mask email address with regular expression

    • Description: Use regular expression to mask the local part of the email address.
    $email = "example@example.com"; $maskedEmail = preg_replace('/(?<=.).(?=[^@]*?@)/', '*', $email); echo $maskedEmail; 

    Explanation: This code snippet uses regular expression to hide the local part of the email address and keeps the domain part visible.

  8. PHP: Secure display of email address with hidden characters

    • Description: Securely display the email address with hidden characters using str_pad function.
    $email = "example@example.com"; list($local, $domain) = explode('@', $email); $hiddenLocal = substr($local, 0, 1) . str_pad('', strlen($local) - 1, '*') . '@' . $domain; echo $hiddenLocal; 

    Explanation: This code snippet uses str_pad function to hide the local part of the email address and keeps the domain part visible.

  9. PHP: Mask email address with simple function

    • Description: Use a simple function to mask the local part of the email address.
    function maskEmail($email) { $parts = explode('@', $email); $local = $parts[0]; $maskedLocal = str_repeat('*', strlen($local)) . '@' . $parts[1]; return $maskedLocal; } $email = "example@example.com"; echo maskEmail($email); 

    Explanation: This function hides the local part of the email address with asterisks (*) and returns the modified email.

  10. PHP: Partially hide email address with custom function

    • Description: Create a custom function to partially hide the email address.
    function hideEmail($email) { $parts = explode('@', $email); $local = $parts[0]; $maskedLocal = substr($local, 0, 2) . str_repeat('*', strlen($local) - 2) . '@' . $parts[1]; return $maskedLocal; } $email = "example@example.com"; echo hideEmail($email); 

    Explanation: This custom function hides part of the local part of the email address with asterisks (*) and returns the modified email.


More Tags

bitcode playframework rxjs6 dart-async stock string-length pem mjpeg ghostscript monaco-editor

More Programming Questions

More Entertainment Anecdotes Calculators

More Financial Calculators

More Electrochemistry Calculators

More Physical chemistry Calculators