PHP code example of tchilly / email-masking

1. Go to this page and download the library: Download tchilly/email-masking library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tchilly / email-masking example snippets




namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Tchilly\EmailMasking\Traits\HasMaskedEmail;

class User extends Authenticatable
{
    use HasMaskedEmail;

    // Make sure to add 'email_masked' to your $appends array
    // if you want it 

$user = User::first();
echo $user->email_masked; // j***@example.com

$user = User::first();
echo $user->getMaskedEmail(); // Default masking (same as email_masked)
echo $user->getMaskedEmail(null, 3); // With max 3 asterisks
echo $user->getMaskedEmail('[email protected]'); // Custom email with default masking

use Tchilly\EmailMasking\Traits\HasMaskedEmail;

$maskedEmail = HasMaskedEmail::maskEmail('[email protected]');
echo $maskedEmail; // j******@example.com

// Customize the maximum number of asterisks
$maskedEmail = HasMaskedEmail::maskEmail('[email protected]', 3);
echo $maskedEmail; // j***@example.com

// In your model
protected function emailMasked(): Attribute
{
    return new Attribute(
        get: fn () => static::maskEmail($this->email, 3) // Always use max 3 asterisks
    );
}