home | contact us
» php » Get Name from Email Address

BLOG CATEGORY: php


Check out this simple and easy function for extracting the name from an email address. It’s not very advanced but it is quite handy. Of course it could be used for similar applications by simply changing the character to split on.

function email_name($email_address, $split='@'){
	return ucwords(strtolower(substr($email_address, 0, strripos($email_address, $split))));
}

There are some very powerful things you can do with PHP and email. Check out this POP3 email class for some ideas. I am just starting to scratch the surface of this functionality and will doubtless have some more great things to show soon.


Click Here to Contact Us about Get Name from Email Address
 

Comments

2 Comments

2 Responses to “Get Name from Email Address”

  1. physio123 says:

    Very great information.

  2. GeekDrop.com says:

    Nice little function. Just a tip, the ucwords PHP function turns all words into “Mixed Case” (All Words Start Capitalized – http://us3.php.net/ucwords) which probably isn’t really needed in email address name extraction since the names are usually one word. If you still want to make that one word’s first letter you could use ucfirst instead. Also, the strtolower isn’t necessary because the ucwords/ucfirst functions will automatically lowercase all except the first letters for you.

    A more efficient function to just extract the name from an email address (without the case changes) would be:

    $emName = explode(‘@’, ‘somename@someemail.com’);
    $emName = $emName[0]; // Returns ‘somename’

    That splits the email into an array using the @ sign as the delimiter; the first item in the array will be what’s before the @ sign, the second item contains what’s after it.

    You can change the casing as needed with something like:

    $emName = ucfirst($emName);
    or even change the second line to: $emName = ucfirst($emName[0]); // Returns ‘Somename’

    Hope that helps!

Leave a Reply

rss icon