dragorim Posted October 14, 2007 Share Posted October 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/73160-solved-text-files-into-array/ Share on other sites More sharing options...
marcus Posted October 14, 2007 Share Posted October 14, 2007 Uh, show us the text file. Quote Link to comment https://forums.phpfreaks.com/topic/73160-solved-text-files-into-array/#findComment-369027 Share on other sites More sharing options...
dragorim Posted October 14, 2007 Author Share Posted October 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73160-solved-text-files-into-array/#findComment-369028 Share on other sites More sharing options...
dragorim Posted October 14, 2007 Author Share Posted October 14, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73160-solved-text-files-into-array/#findComment-369031 Share on other sites More sharing options...
marcus Posted October 14, 2007 Share Posted October 14, 2007 $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. Quote Link to comment https://forums.phpfreaks.com/topic/73160-solved-text-files-into-array/#findComment-369035 Share on other sites More sharing options...
dragorim Posted October 14, 2007 Author Share Posted October 14, 2007 yeah, that just made a bunch of commas. Quote Link to comment https://forums.phpfreaks.com/topic/73160-solved-text-files-into-array/#findComment-369036 Share on other sites More sharing options...
trq Posted October 14, 2007 Share Posted October 14, 2007 <?php $tmp = ''; $file = file('yourfile.txt'); foreach ($file as $line) { $tmp .= str_replace(' ',',',$line); } file_put_contents('newfile.csv',$tmp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73160-solved-text-files-into-array/#findComment-369038 Share on other sites More sharing options...
dragorim Posted October 14, 2007 Author Share Posted October 14, 2007 Wow, that worked so well! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/73160-solved-text-files-into-array/#findComment-369041 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.