1

I have an array which contains the full name. I know how to display the last name which basically resides in the 1st index. How can I display the rest of the values after the last name?

$fullname = array('fullname' => 'POTTER Harry James');
$res = implode("", $fullname);
$name = preg_split("/[\s,]+/", $res);
$lname = $name[0];

What i did in the first name:

$fname = $name[1]. " ".$name[2];

It works fine, but is there a cleaner way to do that? I mean, what if the user has more than 2 first names?

Thanks.

ladybug
  • 65
  • 1
  • 3
  • 10

5 Answers5

3

I suggest to use explode() function:

<?php
$fullname = array('fullname' => 'POTTER Harry James');
$parts    = explode(' ', $fullname['fullname']);

var_dump($parts);
?>

Shows:

array(3) {
  [0]=>
  string(6) "POTTER"
  [1]=>
  string(5) "Harry"
  [2]=>
  string(5) "James"
}

You might use any part of $parts in a way, that you need.

<?php
$a = array_shift($parts);
$b = implode(' ', $parts);

echo "{$b} ({$a})"; // Harry James (POTTER)
?>

UPDv1:

In your current code, you might do just the same thing:

<?php
$lname = array_shift($name);
$fname = implode(' ', $name);
?>
BlitZ
  • 12,038
  • 3
  • 49
  • 68
  • +1 from me. Because this is the easiest way to split other than used in Question. – Smile May 30 '13 at 04:18
  • @CORRUPT: explode won't work. i have tried it countless of times. the problem is that the 2nd parameter should be a string. – ladybug May 30 '13 at 04:19
  • @ladybug , works for me. Just check it. Second argument is a string already. And it should be faster, that your approach (less function calls). – BlitZ May 30 '13 at 04:21
  • @ladybug i think approach in answer is better then regx http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags :) – NullPoiиteя May 30 '13 at 04:53
  • @CORRUPT: thanks for taking time answering my question. appreciate it. – ladybug May 30 '13 at 05:23
  • @CORRUPT: The use of array_shift made more sense, since some of the names have salutations. – ladybug May 30 '13 at 06:01
  • @ladybug: yes it did, indeed, just tweaked it a bit and viola, just what i needed. thanks. – ladybug May 30 '13 at 06:43
2

I think you should take out the last name off the array first, then use a loop to concatenate the remaining names as fistname.

$fullname = array('fullname' => 'POTTER Harry James');
$res = implode("", $fullname);

$name = preg_split("/[\s,]+/", $res);

$lname = array_shift($name);

$fname = "";

foreach($name as $fnames)
    $fname.= " ".$fnames;
BlitZ
  • 12,038
  • 3
  • 49
  • 68
1
$fullname = array('fullname' => 'POTTER Harry James');
$firstNames = strstr($fullname['fullname'], ' '); // $firstNames='Harry James'

This gets the string after the first space character.

Ziarno
  • 7,366
  • 5
  • 34
  • 40
0

You could do

<?php
$fullname = array('fullname' => 'POTTER Harry James');
list($lname,$fname) = explode(" ", $fullname["fullname"],2);
echo $fname."<br>".$lname;
?>

This will work if the name contains 2 words or more. In case there are more than 2 words then anything except the first word will be considered as first name and first word will be considered as the last name.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
0

This is a bit of an impossible task. If left to their own devices, users are going to put all sorts of stuff into a single field.

If you care about separating the first and last names of your users, you should ask for them separately, as there is no regex that can possibly determine the correct way to break up their name.

Chris Henry
  • 11,914
  • 3
  • 30
  • 31