I am a very beginner in php. I want to extract the first name and the last name from the full name. The following was my code :-
$str= "fname lname";
This is a quick and easy way.
$name = 'John Smith'; list($firstName, $lastName) = explode(' ', $name); echo $firstName; echo $lastName;
But if you want to take into account middle names etc,
$name = 'John Mark Smith'; $name = explode(' ', $name); $firstName = $name; $lastName = (isset($name[count($name)-1])) ? $name[count($name)-1] : '';
The second approach would be better, as it will work for 1 name too.