Laravel 9 Partially Hide or Mask Email Addresses Tutorial

Reading Time: 5 minutes
1,134 Views

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

If you are looking for a solution i.e Hint or partially hide email address with stars (*) in Laravel 9 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: CodeIgniter 4 Partially Hide or Mask Email Addresses Tutorial

We will use the concept of Laravel 9 helper here to see this concept.

Examples

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

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

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

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Helper File

In laravel 9, there is no specific folder location defined in application structure to store custom helper files.

So we can create anywhere either at root or inside /app folder.

Let’s create a file helpers.php inside /app folder.

Read More: PHP How To Partially Hide or Mask Email Addresses Tutorial

We have several different codes by which we can mask email addresses. We will see two methods.

Method #1

Open helpers.php from /app folder and write this code into it.

<?php

// Mask email address with (*) stars
// Example: onl**************@g****.com

if (!function_exists('hideEmailAddress')) {

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

Method #2

Open helpers.php from /app folder and write this code into it.

<?php

// Mask email address with (*) stars
// Example: onlinewe********@gmail.com

if (!function_exists('hideEmailAddress')) {

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

Add into composer.json File

We need to include file helpers.php file into composer.json (you will find this file at application root). Once you add this line into autoload, custom helper file will be autoload once application executes.

...

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files": [
         "app/helpers.php"
     ]
}

...

Regenerate All Classes in Application

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

Open project into terminal and run this command.

$ composer dump-autoload

This will dump your application’s composer.json and loads all autoload classes once.

Usage of Helper Function

Now, we will see how to use custom helper function in application.

Using into View

<div>
     @php
        $email = hideEmailAddress('onlinewebtutorhub@gmail.com'); // Method #1 function
     @endphp

        {{ $email }}
 </div>

Output

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

Using into Controller

//...

$email = hideEmailAddress('onlinewebtutorhub@gmail.com'); // Method #1 function

echo $email;

//...

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

Using into Closure Routes

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
  
    $email = hideEmailAddress('onlinewebtutorhub@gmail.com'); // Method #1 function

    echo $email;
});

Output

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

We hope this article helped you to learn about Laravel 9 Create Custom Helper Functions Tutorial in a very detailed way.

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.