This topic is locked

Trying To Make Lastname, Firstname --> Firstname, Lastname

4/8/2013 11:08:37 PM
PHPRunner General questions
K
karmacomposer author

I have a database where the full name came in as lastname, firstname mi.
I want to be firstname mi lastname with no comma. The full name is in one field.
Here is the code I wrote to reverse the names. It does not work. Please help.



$value = $data["name"];

$names = explode(", ", $value);

$value = $names[1] . " " . $names[0];


Not sure how to deal with the middle initial.
Anyone know how to write this so it works?
Mike

Sergey Kornilov admin 4/9/2013

It all depends on data format in the database. This code should work if last name is always followed by comma and space. Any other formatting - it won't work. It on't handle middle names either. You need to write the code that will work with each possible format.