1

I have an array that is used to get a persons name and i have the variable below which will give me the Full name name the user. e.g. John Smith. How do i split this array in to two variables.

e.g. $fb_firstname and $fb_lastname?

$fb_name = $user_profile['name'];
1
  • All the answers below are good when the pattern is {FName LName} and no middle initials. Commented Dec 26, 2011 at 20:54

3 Answers 3

2
$arr = explode(' ', $fb_name);
$fb_firstname = $arr[0];
$fb_lastname = $arr[1];
Sign up to request clarification or add additional context in comments.

Comments

1

use the explode function. and then assign output array [0] element as $fb_firstname and [1] element as $fb_lastname.

Comments

1

You can use explode­Docs for that and a space as delimiter:

$fb_name = $user_profile['name'];
list($fb_firstname, $fb_lastname) = explode(' ', $fb_name, 2) + array('', '');

The important part with that code is that the name needs to have one space only between first and last name and that the firstname comes first.

If there is no lastname, $fb_lastname will be an empty string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.