This works rather nicely, too:

<?php
$list = "Firstname1 LastName1
Firstname2 Lastname2";

$name_array = array();
$temp_array = explode("\n", $list);
foreach ($temp_array as $name) {
        $name_array[] = explode(" ", $name);
}

print_r($name_array);
?>

$name_array is now a multi-dimensional array with the first and last names separated. print_r should show you the values.


Richard Davey wrote:
Hello Jeff,

Tuesday, March 16, 2004, 3:24:57 PM, you wrote:

JO> If I have a list like this:
JO> Firstname1 LastName1
JO> Firstname2 Lastname2
JO> etc.

JO> I can split the names by \n to put them into
JO> an array which will then be output into an <option>
JO> list for a form. But how do I reverse the names
JO> so that the last name is first? Also, how do I
JO> handle people with three names? Thanks.

<?
function swap (&$name)
{
        $tmp_name = trim($name);
        $pos = strrpos($tmp_name, " ");
        $first_name = substr($tmp_name, 0, $pos);
        $surname = substr($tmp_name, $pos);
        $name = "$surname, $first_name";
}

$raw_names = "richard davey
susannah davey
jeff oien
willy three names
jack van-der-meet";

$names = explode("\n", $raw_names);

echo "<pre>";
print_r($names);

array_walk($names, 'swap');

print_r($names);
echo "</pre>";
?>

The echo and print_r lines are to show you what it has done.
The $raw_names should be your list of names.


-- Regards, Ben Ramsey http://benramsey.com http://www.phpcommunity.org/wiki/People/BenRamsey

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to