Last active
April 25, 2016 07:37
-
-
Save stevenkellow/d15432d0bdba9ad33504d19bea1205db to your computer and use it in GitHub Desktop.
Get names from email address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById('email').onchange=get_names; // When an input with id email is completed we can get to work! | |
function get_names(){ // Calls function | |
var email = document.getElementById('email').value; // Gets what was entered into email e.g [email protected] | |
var parts = email.split("@"); // Splits the email at the @ symbol | |
var username = parts[0]; // Can be used as a username if you'd like, but we'll use it to find names anyway | |
var delimiters = [".", "-", "_"]; // List of common email name delimiters, feel free to add to it | |
delimiters.forEach(splitting_loop); | |
function splitting_loop(element, index, array) { // Checks all the delimiters | |
var parts_name = username.replace(/\d+/g, ''); // Remove numbers from string | |
var num = parts_name.indexOf(element); // Gets the position of the delimiter | |
if (num > -1) { // If there is a delimiter | |
fname = parts_name.substring(0, (num)); // Gets data before delimiter | |
lname = parts_name.substring(num + 1, (parts_name.length)); // Gets name after delimiter | |
fname = fname.toLowerCase(); // Makes it all lower case | |
lname = lname.toLowerCase(); | |
fname = fname.charAt(0).toUpperCase() + fname.slice(1); // Capitalise first letter as well so it looks prettier | |
lname = lname.charAt(0).toUpperCase() + lname.slice(1); | |
} | |
} | |
if (typeof fname !== 'undefined') { // If we've found a delimiter we can use it | |
/* This code just shows what you can do with the names, but you can use it for something more interesting! */ | |
window.alert(fname + ' ' + lname); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$email = "[email protected]"; // Or Use $_GET['subject']; / $_POST['subject']; depending on form | |
$parts = explode("@",$email); // Splits the email at the @ symbol | |
$username = $parts[0]; // Can be used as a username if you'd like, but we'll use it to find names anyway | |
$delimiters = array(".", "-", "_"); // List of common email name delimiters, feel free to add to it | |
foreach ($delimiters as $delimiter){ // Checks all the delimiters | |
if ( strpos($username, $delimiter) ){ // If the delimiter is found in the string | |
$parts_name = preg_replace("/\d+$/","", $username); // Remove numbers from string | |
$parts_name = explode( $delimiter, $parts_name); // Split the username at the delimiter | |
break; // If we've found a delimiter we can move on | |
} | |
} | |
if ( $parts_name ){ // If we've found a delimiter we can use it | |
$fname = ucfirst( strtolower( $parts_name[0] ) ); // Lets tidy up the names so the first letter is a capital and rest lower case | |
$lname = ucfirst( strtolower( $parts_name[1] ) ); | |
/* This code just shows what you can do with the names, but you can use it for something more interesting! */ | |
echo $fname . ' ' . $lname; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment