How to extract username from an e-mail address string
Posted on September 5, 2008, under PHP,
Bookmark it
If you need to extract the username from an e-mail address string then this snippet can help you. For instance, you can use this function inside a loop, while selecting emails from a database.
<?php
function getUsernameFromEmail($email)
{
$find = '@';
$pos = strpos($email, $find);
$username = substr($email, 0, $pos);
return $username;
}
$email = 'thecoder@domain.com';
$username = getUsernameFromEmail($email);
echo $username; // thecoder
?>
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- September 5, 2008
- article by Gabriel C.
- 2 comments
Related Posts
-
How to extract domain name from an e-mail address stringat September 5, 2008 with 1 comment
-
PHP: Convert E-Mail Addresses from a String into Clickable Linksat October 6, 2008 with 2 comments
-
PHP: How to validate e-mail addresses using regular expressionsat May 21, 2008 with 2 comments
-
Validate (input) usernameat August 29, 2008 with 2 comments
-
PHP: How to extract numbers from a string (text)at October 5, 2008 with 7 comments

2 Replies to "How to extract username from an e-mail address string"
July 24, 2009 at 11:01 AM
$mail = ‘test@domain.com’;
list( $username , $domain ) = explode( ‘@’ , $mail );
echo $username; // Prints “test”
July 24, 2009 at 11:07 AM
Yes, that is a good alternative method to extract the ‘username’. There are also other ones too.