I am getting a textbox value from a form, i which the customer inputs his full name.
So it is FirstName
and LastName
, Now when he inputes his/her name in the textbox the value is sent through $_POST
and get stored in a variable $name
.
Now i have two fields in my database, one is first_name
and another is last_name
.
The last_name
will never contain any value which includes space.
For example: it cant be Doe Doe
, it has to be Doe
only.
Where as in first_name
field, spaces are allowed, For example user name could be John
or John Mennon
.
Therefore we can say that we have a first_name
in mysql table field John Mennon
and last_name
be Doe
.
Now when user input this value as John Mennon Doe
, i want to get the last value after spaces as my last_name
i.e. Doe
and remain all to be my first_name
i.e. John Mennon
.
How can i get these two values out of the submitted value stored in a variable called $name
?
As after getting these values i'll match them both separately from mysql database and let user do the other job.
$parts = explode(' ', trim($_POST['name']));
$lastName = array_pop($parts);
$firstName = implode(' ', $parts);