How to Extract the User Name from an Email ID Using PHP

In PHP, the best way to extract the username from an email is using the explode() function.

The explode() function splits a string by a specified delimiter and returns an array of strings. For email extraction, you can split the string at the ”@” character.

explode(”@”, $email): The username will be the first element of the array returned by explode().

Here’s a simple example demonstrating how to do this:

<?php
// Sample email address
$email = "[email protected]";

// Split the email address by the "@" character
$parts = explode("@", $email);

// The username is the first part
$username = $parts[0];
echo "Username: " . $username;

Output:

Username: john123