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 508k
  • Answers 508k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You must use the fully qualified name (including the namespace)… May 16, 2026 at 4:13 pm
  • Editorial Team
    Editorial Team added an answer When you start mongod (the MongoDB daemon), it starts listening… May 16, 2026 at 4:13 pm
  • Editorial Team
    Editorial Team added an answer Dispatch a custom event (like imgLoaded) from the image loading… May 16, 2026 at 4:13 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

Related Questions

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago\n
I've wrote some code that will check two dates - they are split into
I wrote the basic code below and saved to a file called pdf.java. package
Good Morning, I wrote the code block below on my local Windows 7 PC
Im trying to draw a line. I wrote a code like below. UIColor *currentColor
I have created a file with printStream object as shown below. PrintStream outPutOffice =
when I run the javascript code below, I get the variable original as ending
Please see code below: s = new Socket(nextHopIP, 3434); fileIn = new FileInputStream(fileName); fileOut
I have a bit of code below that creates a button on a html
I was wondering if anyone could look over a class I wrote, I am

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.