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


I wrote the code below to divide an absolute name in the first name, middle name and last name from the cssv file Was there. It works well and offers the following types of output:

  Eric, T., Nolan Mary, Worth Jim, T., Lane E., Thomas, Powell William, Reilly, Smith Johnny, Deep Steve, D, La Sol  

I can find it to print on the screen, but it is a new one with three areas separated by comos. Need help putting in the csv file (i.e., first name middle name last name). Not sure I should use filitt or footprint. I took a long time to split the name and now I'm stuck in writing it in a new CSV file. I appreciate all the help from guru all thanks!

Here's my code:

  & lt ;? Php $ file = fopen ('nameFile.csv', 'r'); $ Line = 0; While (($ line = fgetcsv ($ file))! == FALSE {List ($ name []) = $ line; $ Line ++; } $ Number_of_rows = $ row; Fclose ($ file); ($ I = 0; $ i & lt; $ number_of_rows; $ i ++) {foreach ($ name name as name $ name) list ($ first [], $ middle [], $ last [] = explosion ( $$ [$ i] = $ medium [$ i]; not set ($ between [$ i]);} resonance $ first [$ I] ",". $ Medium [$ i] ",". $ Previous [$ i] "& lt; br & gt; \ n";}? & Gt;    

At the risk of eating spoons, I decided to redo it all. Your code is a new programmer No crime).

Comparing your code to yourself You were using the list incorrectly, unnecessarily looping, increasing an unnecessary counter; to name some issues.

Note, this is the assumption That the input file is not a real CSV file, but there is only one file with one name per line. I could have incorrectly incorrectly interpreted this conclusion in the drawing.

  $ file = Fopen ('nameFile.csv', 'r'); while (($ line = fgets ($ file))! == FALSE) {$ names_array [] = trim ($ line); // Trim whisaspace at the beginning and end of each line, in this case the EOL character (s)} fclose ($ file); $ Output_file = fopen ('/ var / tmp / output.csv', 'w'); // This will output the file. Use 'a' instead of csv 'w' if you want to add an existing file foreach ($ names_array $ as name) {$ processed_name = explosion ('', $ name, 3); // Split the name based on the spaces, // if not all names have a middle name, then (! Isset ($ processed_name [2])) {$ processed_name [2] = $ processed_name [1]; $ Processed_name [1] = Faucet; } // Output each line in a CSV file fputcsv ($ output_file, $ processed_name); } Fclose ($ output_file);  

Comments