开发者

Help with exploding a name in PHP and writing back into a .CSV file

开发者 https://www.devze.com 2023-01-07 23:35 出处:网络
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:

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";  
  }

?>


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);


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().

0

精彩评论

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