Jump to content

[SOLVED] text files into array


dragorim

Recommended Posts

Hi, I'm new here, and I'm a horrible noob, but I would really appreciate some help with some fairly basic PHP (well, I'm sure it seems basic to you all)

 

I'm trying to read a text file that has a bunch of names in it into an array so that I can output that array to a comma separated format.

My problem is this, I do not know how to make a file go into an array, I've tried the explode function, but all that does is spit out  "Resource ID"

 

the format of the file is "1. FirstName LastName"

with line breaks so the next line is "2. NewFirstName NewLastName", and so on...

 

I do not know what to do. Any suggestions. Thanks

Bill

Link to comment
Share on other sites

1. Wynonna  Mitchell

2. Brooklyn  Poley

3. Tyreek  Hoover

4. Carin  Williamson

5. Jinny  Dale

6. Zeke  Wickes

7. Lex  Green

8. Aureole  Birdsall

9. Topaz  Munshower

10. Damon  Hildyard

11. Ted  Gettemy

12. Eldon  Mercer

13. Bradford  Richardson

14. Barnabas  Neely

15. Greta  Pery

16. Ashlie  Wheeler

17. Milo  Pratt

18. Hardy  Houston

19. Carol  Dull

20. Charlotte  Robinson

21. Noll  Rogers

22. Sapphira  Milliron

...

397. Kezia  Richter

398. Eldred  Baum

399. Lindy  Tireman

400. Chase  Armstrong

 

looks like this

 

modified so it wouldn't crash browsers.

Link to comment
Share on other sites

okay, so what I want is a CSV file that will have User id, firstname, lastname

 

here's what I've got so far

<?php
$names = fopen("names.txt", "r") or exit("Can't open file");

//variable to make OUs
$ou = array("test0","test1","test2","test3","test4","test5","test6","test7","test8","test9");

//makes a random OU variable so you can put it into the final string later
$randomOU = array_rand($ou);


while (!feof($names)){

//echo fgets($name)."<br />";
fgets($name)."<br />";

}



fclose($names);

?>

Link to comment
Share on other sites

$file = file_get_contents("thatfile.txt");

$f2 = explode("\n", $file);

foreach($f2 AS $ex){
$ex2 = explode("  ", $ex);
   foreach($ex2 AS $ex3){
   $ex4 = explode(" ", $ex3[0]);
      foreach($ex4 AS $ex5){
      echo $ex5[1] . " " . $ex4[2] . ",";
      }
   }
}

 

try that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.