Just a little bit of code here, thought it would be useful to share.
If you’re ever working with a system that registers users and would like to know user’s names to personalise things a little, you’ll understand the frustration of people not co-operating in giving you their names!
These simple scripts, either Javascript or PHP, can be added on to your registration page and will let you pick up first names and surnames from email addresses. Hope it helps!
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 john-smith@hotmail.co.uk | |
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); | |
} |
<?php | |
$email = "john_smith@hotmail.com"; // 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; | |
} |
Feel free to test out the code and see how you can make it work for you on JSFiddle, where there’s an easy to follow JavaScript example that shows how you can make form registration easy, or the online PHPtester website, where you can try out some custom PHP code.
also you can try https://wtools.io/php-sandbox – like php tester, but better 🙂 You can share your code;
Yeah I agree on that actually. The resource I use now is implode.io – that’s a bit of a cleaner design, allows you to save examples, and it includes some support for Laravel etc. too.