Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 934539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:01:10+00:00 2026-05-15T21:01:10+00:00

I wrote the code below to split up a fullname from a .csv file

  • 0

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

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:01:12+00:00Added an answer on May 15, 2026 at 9:01 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 458k
  • Answers 458k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The best solution would be Command Patter as igorbel said,… May 15, 2026 at 11:14 pm
  • Editorial Team
    Editorial Team added an answer I would try using a regular expression to replace all… May 15, 2026 at 11:14 pm
  • Editorial Team
    Editorial Team added an answer don't bind it on option $("#multiple_select").click(function(){ alert("works"); }); accepted answer:… May 15, 2026 at 11:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.