views:

68

answers:

2

I wrote the code below to split up a fullname from a .csv file into a first name, middle name, and last name. It works well and gives the following kind of output:

Eric,T.,Nolan
Mary,,Worth
Jim,T.,Lane
E.,Thomas,Powell
William,Reilly,Smith
Johnny,,Depp
Stevie,de,la Soul 

I can get it to print to the screen, but need help putting it back in a new .csv file with three fields separated by commas (i.e., firstname, middlename, lastname). Not sure if I should use fwrite or fputcsv. Took me a long time just to split the name and now I'm stuck on writing it back to a new csv file. I'd appreciate some help from the gurus. Thanks all!

Here's my code:

<?php

$file = fopen('nameFile.csv', 'r');
$row = 0;
while (($line = fgetcsv($file)) !== FALSE)
{
list($name[]) = $line;
$row++;
}
$number_of_rows = $row;
fclose($file);

for($i = 0; $i < $number_of_rows; $i++) {
foreach ($name as $NameSplit)
  list($first[], $middle[], $last[]) = explode(' ', $NameSplit, 3);
  if ( !$last[$i] ) {
    $last[$i] = $middle[$i];
    unset($middle[$i]);
  } 
echo $first[$i] . "," . $middle[$i] . "," . $last[$i] . "<br>\n";  
  }

?>
A: 

That last one doesn't look right. "Stevie de la Soul", last name should be "de la Soul" and middle name should be empty. Also, there are many cultures where people don't have last names.

In any event, to write a CSV back to file, just use the fputcsv() function. It's listed as a "See alse" on the manual page for fgetcsv().

bluesmoon
Good point bluesmoon. I know I'll probably have to make some manual adjustments with the output, but I can live with it.
Thomas
+1  A: 

At the risk of spoon feeding you, I've decided to redo it all. Your code shows the hallmarks of a new programmer (no offense).

Compare my code to your own. You were using list incorrectly, looping unnecessarily, incrementing an unnecessary counter; to name a few issues.

Note, this hinges on the assumption that the input file isn't an actual CSV file, but simply a file with one name per line. I may have misinterpreted your code in drawing this conclusion.

$file = fopen('nameFile.csv', 'r');

while (($line = fgets($file)) !== FALSE)
{
    $names_array[] = trim($line); // trim whitespace at the begining and end of each line, in this case the EOL character(s)
}

fclose($file);

$output_file = fopen('/var/tmp/output.csv', 'w');  // this will clobber the file output.csv  use 'a' instead of 'w' if you want to add to an existing file

foreach ($names_array as $name)
{
    $processed_name = explode(' ', $name, 3); // split the name, based on spaces

    // not all full names contain a middle name     
    if (!isset($processed_name[2]))
    {
        $processed_name[2] = $processed_name[1];
        $processed_name[1] = null;
    }

    // output each line into a CSV file
    fputcsv($output_file, $processed_name);
}

fclose($output_file);
George Marian
Yep, I'm a total noob hack. But I actually enjoy it a bit as a (gasp!) hobby. This works great. Yes, my input file is one name per line. Just a followup - was I on the right track if I do have a csv input file? With, say: name, address, age. On your prior answer I couldn't figure out how to put the following into a variable, and then into a csv using fputcsv: $first[$i] . "," . $middle[$i] . "," . $last[$i] . "<br>\n"; But your code works great! Thanks again George
Thomas
@Thomas Yeah, you were on the right track and this can be expanded to handle that situation. You'll just need to keep in mind that `$names_array` would contain a bunch of arrays, one for each line. My previous answer was a quick stab in the dark and had problems. The `$out` array would contain one line per entry, though it wouldn't quote the values correctly for a CSV file. It could just be written to a file one line at a time. Your basic issue with `$first $middle $last` is that you're using an array incorrectly. Have fun.
George Marian