Grabeorama Posted January 7, 2009 Share Posted January 7, 2009 Is there a way of alphabetizing a list of words in a text file?Like if I had:'animals.txt'DogCatMouseLionHow would I make that text file look like this:CatDogLionMouseThanks~Grabeorama Link to comment Share on other sites More sharing options...
jeffman Posted January 7, 2009 Share Posted January 7, 2009 Use file() to read the file into an array.Use sort() to alphabetize the array. -- But investigate what natsort() and natcasesort() do.Use implode() to turn the array into a stringWrite the string to a file using any of several methods. file_put_contents() works if you have PHP 5 or better. Otherwise use fopen() and fwrite(). Link to comment Share on other sites More sharing options...
Grabeorama Posted January 7, 2009 Author Share Posted January 7, 2009 Ok, thanks I'll try thatEDIT:This is my code: <html> <head> <title>Test page</title> </head> <body> <p>Test Page</p> <?php $text = 'animals.txt'; $new_array = file($text); $new_array = natcasesort($new_array); $new_array = implode($new_array); echo $new_array; ?> </body></html> and here is the output I got:Test PageArrayand here's the contents of 'animals.txt':"Dog, Cat, Mouse, Lion, Tiger, Rat"Is there a certain way of writing animals.txt? Do I leave out the comas? Link to comment Share on other sites More sharing options...
Ingolme Posted January 7, 2009 Share Posted January 7, 2009 the file() command reads each line of a document as an element of the array, so separate the elements with line breaks, not commas.If you put them all on one line separated by commas you'll have to use a different method to extract it from the file and turn it into an array.When using implode() specify a separating character, like "," or a line break: implode(",",$file_array); Otherwise no character will be put between the elements. Link to comment Share on other sites More sharing options...
Grabeorama Posted January 28, 2009 Author Share Posted January 28, 2009 OK, this is my new code, but still returns an error: <html><head> <title>Array Sort</title></head><body> <?php $file = "review_list_a.txt" $new_array = file($file); $new_array = natcasesort($new_array); $new_array = implode(",",$new_array); echo $new_array; ?></body></html> It returns:Parse error: parse error, unexpected T_VARIABLE in /home/content/k/g/r/kgrabe/html/grabeorama/test.php on line 10Any ideas as to why?Thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted January 28, 2009 Share Posted January 28, 2009 There's no semicolon after the first line. Link to comment Share on other sites More sharing options...
Grabeorama Posted January 28, 2009 Author Share Posted January 28, 2009 Woops ThanksI still get an error though:Warning: implode(): Bad arguments. in /home/content/k/g/r/kgrabe/html/grabeorama/test.php on line 12 Link to comment Share on other sites More sharing options...
jeffman Posted January 28, 2009 Share Posted January 28, 2009 var_dump($new_array) right after you read the file. See if it holds what it's supposed to. Link to comment Share on other sites More sharing options...
jeffman Posted January 28, 2009 Share Posted January 28, 2009 NOPE. This line:$new_array = natcasesort($new_array);Should be this:natcasesort($new_array);It acts directly on the original and returns a Boolean (which you can check if you want). Link to comment Share on other sites More sharing options...
Grabeorama Posted January 28, 2009 Author Share Posted January 28, 2009 Ok, no errors now :)but I'm not sure if it sorts the text right. I'll try again tomorrow and let you know :)EDIT: No, the text doesn't get sorted alphabetically Is there something wrong with the code? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.