PHP Explode to MySQL

Soldato
Joined
28 Sep 2008
Posts
14,158
Location
Britain
Google is not quite returning anything useful, perhaps I'm searching for the wrong thing.

Basically, I have a form. Username, Email and Password.
I have a MySQDB with id, username, firstname, lastname, password

When the user registers using a strict (corporate) email, I would like the PHP to handle exploding the email (delimiting at the .) to automagically populate the database firstname and lastname.

ie, User form shows:

Username: django x2
Email: [email protected]
Password: something

Then on submit, the DB will be populated with

Username: django x2
Firstname: django
Lastname: x2
Email: [email protected]
Password: hashvalue

Alternatively, I guess it would be ok for a SQL timer job to come along and do that work (if Explode or a similar functions works in there).

TIA
 
Soldato
Joined
6 Mar 2008
Posts
10,079
Location
Stoke area
daft question but why not save yourself the hassle and just put a first name/surname option on the registration form?

Having had a quick look at explode, it looks like you'd have to explode first on the @ symbol then use explode on the first element of the output array on the fullstop.

You'd then have a second array of 3 parts, firstname, fullstop, surname.
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
Explode will take your input string and output an array of strings. Then you need to address each string in the array that you want, something like this:

email_address = <value from form input>
exploded_array = explode(".", email_address);
first_name = exploded_array[0];
last_name = exploded_array[1];
 
Soldato
Joined
18 Oct 2002
Posts
14,061
Location
Sandwich, Kent
I'd do it dynamically in the UI. When the user has entered an email (onBlur), if the name / surname boxes are blank, try to populate them with data from the email (given the expected format).

This then gives the option of whoever is entering the text to override it, incase their email isn't their 'go by' name. Eg Bob, instead of Robert, or Rik instead of Richard. Or if there are any exceptions (eg John.Smith2) to the standard rule.
 
Soldato
Joined
18 Oct 2002
Posts
14,061
Location
Sandwich, Kent
Explode will take your input string and output an array of strings. Then you need to address each string in the array that you want, something like this:

email_address = <value from form input>
exploded_array = explode(".", email_address);
first_name = exploded_array[0];
last_name = exploded_array[1];
Great if you want the last name to be smith@company :p
 
Permabanned
Joined
9 Aug 2009
Posts
12,234
Location
UK
in a corporate environment emails do not necessarily match their names, example reasons:
- two people with the same name
- marriage

people can get touchy if you mess up their name, so best just give them a box to type it in.

and don't do first name + last name because asians.
 
Soldato
OP
Joined
28 Sep 2008
Posts
14,158
Location
Britain
Some interesting options.

To answer a few

1). There is no ambiguity on emails as fundamentally, the application will only ever return the firstname from the split string
2). That kind of answers the split(explode) at the period, because really, everything after the @ can be discarded
3). Our users are dumb, we'd get stupid Firstnames which would make identifying the user harder, so unless the form can dynamically fill in the firstname, but block it from being edited, then I would rather the user could not enter or choose a Firstname
 
Soldato
OP
Joined
28 Sep 2008
Posts
14,158
Location
Britain
$email = "[email protected]";
$firstname = explode(".", $email)[0];
$lastname = explode("@", explode(".", $email)[1])[0];
$username = $firstname . " " . $lastname;

Thanks, although the username is not their email address (without the domain) that's something different (confusing I know).

All that needs to happen is the DB fields need to populate a firstname and a lastname, even though the form doesn't explicitly ask for those. They are derived from the explode and inserted into the DB when the form is submitted.
 
Back
Top Bottom