shage Posted July 28, 2010 Share Posted July 28, 2010 Im trying to figure out the best way to random a line from a text file and after the random delete that line from the file. This way the same username cannot be picked twice thanks for any input Quote Link to comment https://forums.phpfreaks.com/topic/209136-random-line/ Share on other sites More sharing options...
lemmin Posted July 28, 2010 Share Posted July 28, 2010 You pretty much have to read in the entire file (if you don't know how many total lines there are) so you might as well just use file_get_contents: $unames = explode("\n", file_get_contents('file.txt'); $rand = array_rand($unames); //[...] array_splice($unames, $rand); file_put_contents('file.txt', implode("\n", $unames)); Quote Link to comment https://forums.phpfreaks.com/topic/209136-random-line/#findComment-1092245 Share on other sites More sharing options...
wildteen88 Posted July 28, 2010 Share Posted July 28, 2010 If each username is on its own line, then use file to read the text file. To pick a random username you'd use array_rand. $usernames = file('usernames.txt'); // get a random username $random_line = array_rand($usernames); $random_username = $usernames[$random_line]; echo 'Current Username: ' . $random_username; // remove the random username unset($usernames[$random_line]); // rewrite the usernames back to the text file. $handle = fopen('usernames.txt', 'w'); $data = implode($usernames); fwrite($handle, $data); fclose($handle); Quote Link to comment https://forums.phpfreaks.com/topic/209136-random-line/#findComment-1092252 Share on other sites More sharing options...
Psycho Posted July 28, 2010 Share Posted July 28, 2010 Arrays have many useful functions that are rarely used. One example is array_shift() which will remove and return the first value int he array. You could use that (or array_pop() for th elast element) after randomizing the array. I think WildTeens' solution might be more efficient but I supply this as an alternative. //Import file as an array by lines $usernames = file('usernames.txt'); //Randomize the array shuffle($usernames); //Remove the first element $random_username = array_shift($usernames); echo 'Random Username: ' . $random_username; //Rewrite remaining array back to file $handle = fopen('usernames.txt', 'w'); fwrite($handle, implode('\n', $usernames); fclose($handle); Note, the previous solution used implode() without the parameter for the joining string. I don't think the rewritten values would be on separate lines in that instance. I modified it in my solution. If you go with the previous code you may need to modify that line. Quote Link to comment https://forums.phpfreaks.com/topic/209136-random-line/#findComment-1092267 Share on other sites More sharing options...
AbraCadaver Posted July 28, 2010 Share Posted July 28, 2010 I tend to use mjdamato's method, with the following exceptions: 1. Don't implode() with a newline. file() returns each element with a newline unless you pass the FILE_IGNORE_NEW_LINES flag, which might be a good thing to do here if you don't want to trim() the username. 2. I use file_put_contents() anytime it works for the situation. Quote Link to comment https://forums.phpfreaks.com/topic/209136-random-line/#findComment-1092282 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.