开发者

Convert a comma-delimited list of full names into an array

I have a string such as: Crosby Bing, Gretzky Wayne, Clemente Rob开发者_StackOverflow中文版erto

I would like to have the string converted to an array so it looks like:

LName[0]=>Crosby 
FName[0]=>Bing, 
LName[1]=>Gretzky 
FName[1]=>Wayne, 
LName[2]=>Clemente 
FName[2]=>Roberto

I am terrible with arrays and string manipulation and have done many searches on the web but can't locate the appropriate solution.

I look forward to your response!


$nameString = 'Crosby Bing, Gretzky Wayne, Clemente Roberto';
$names = array_map('trim', explode(',', $nameString));

foreach ($names as &$name) {
  $name = explode(' ', $name, 2);
}

Its slightly different from what you want. Its an array of arrays, where the inner arrays has 2 values with the first one the first and the second value is the second (and any further) name. It should not be difficult to convert it into your structure, if even required.

echo $name[0][0]; // Crosby
echo $name[0][1]; // Bing

Kevin Peno suggest something like this in the comments below

$nameString = 'Crosby Bing, Gretzky Wayne, Clemente Roberto';

foreach (explode(',',$nameString) as &$name) {
  $name = explode(' ', trim($name), 2);
}

However, they both are semantic equivalent and as long as there are not 1M names in the string (which will also consumes "1M * average-string-length Byte" of memory), I dont think anyone may realise any performance difference.


Try this out:

<?php

$LName = array();
$FName = array();

list($LName[0],$FName[0],$LName[1],$FName[1],$LName[2],$FName[2]) = 
      explode(" ","Crosby Bing, Gretzky Wayne, Clemente Roberto");

print_r($LName);

print_r($FName);

?>

demo: http://codepad.org/g4YnwKxW

Or if your list is dynamic:

<?php

    $LName = array();
    $FName = array();

    $fullNames = 
          explode(", ","Crosby Bing, Gretzky Wayne, Clemente Roberto");

    foreach($fullNames as $full){
         list($LName[], $FName[]) = explode(" ", $full);
    }

    print_r($LName);

    print_r($FName);

    ?>

demo: http://codepad.org/WMIx9mJD


Done in one line!

preg_match_all('/(\w+) (\w+)(?:, )?/', "Crosby Bing, Gretzky Wayne, Clemente Roberto", $a);

/*
    $fName = $a[1];
    $lName = $a[2];
*/


I think explode() can be useful.

Example:

Things used: explode() var_dump()

$name = 'Crosby Bing';
$splittedName = explode(' ', $name);

var_dump($splittedName);

Should return:

[0] => 'Crosby',
[1] => 'Bing'

And splitting list of names is just simple loop:

Things used: explode() foreach reference

$names = 'Crosby Bing, Gretzky Wayne, Clemente Roberto';
$namesArray = explode(',', $names);

foreach($names as &$name)
{
    $name = explode(' ', trim($name));
}

And simple function:

Things used: explode() Function creation count() array_slice()

function GetNames($name)
{
    $names = explode(' ', $name);
    $namesCount = count($names);

    $data = array();
    $data['Names']  = array_slice($names, 0, $namesCount - 2);
    $data['Surname'] = $names[$namesCount - 1];

    return $data;
}


You need to do two operations. First to split the initial string into an array of names and then work through that array and split each name into firstname and lastname.

The following code shows that but works only under the premise that a full name contains only of a firstname and lastname and both are sperated by a single space.

$allNames = 'Crosby Bing, Gretzky Wayne, Clemente Roberto'; // your string
$names = explode(', ', $allNames); // create an array with all names
$LName = $FName = array(); // initialize your result arrays
foreach($names as $name) {
    list($firstName, lastName) = explode(' ', $name, 2); // split each name apart
    $FName[] = $firstName; // add first name to result array
    $LName[] = $lastName; // add last name to result array
}

Explode is your friend here ;)


You could use the explode function, like so:

$names_string  = "Crosby Bing, Gretzky Wayne, Clemente Roberto";
$names = explode(",", $names_string);
echo $names[0]; // displays Crosby Bing
echo $names[1]; // displays Gretzky Wayne


Demo

http://codepad.org/P4vSIgKP

Code

$str = 'Crosby Bing, Gretzky Wayne, Clemente Roberto';
$arr1=explode(',', $str);

foreach ($arr1 as $item){
   $arr2 = explode(' ', trim($item));
   $lname[]=$arr2[0];
   $fname[]=$arr2[1];       
}

print_r($lname);
print_r($fname);

Result

Array
(
    [0] => Crosby
    [1] => Gretzky
    [2] => Clemente
)
Array
(
    [0] => Bing
    [1] => Wayne
    [2] => Roberto
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜