I have created a form开发者_如何学C that has a name and email address input.
Name: John Smith
Email: johnsmith@example.com
What I want to do is split the name in to first name and surname from the posted.
How would I go about doing this?
Assuming you are escaping user input and there is a validation to add space between name.
If that is already not implemented you could implement them otherwise consider to use separate input box for both first name and last name.
$name=explode(" ",$_POST['name']);
echo $name[0]; //first name
echo $name[1];// last name
$user = 'John Smith';
list($name, $surname) = explode(' ', $user);
$name_array = explode(" ",$name);
A quick way is to use explode(). But note that this will only work if there is only one space in it.
$name = "John Smith";
$parts = explode(' ', $name);
$firstName = $parts[0];
$surname = $parts[1];
精彩评论