Jump to content

Random line


shage

Recommended Posts

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));

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.