Jump to content

PHP Code for randomizing contents of a .txt?


Recommended Posts

Hello, I am looking to randomize contents of a .txt document. I found a code that I think might work, but how can I alter it so it works with what I'm looking to do?

$f_contents = file("TheRandomContent.txt");print_r($f_contents);//$line = $f_contents[array_rand($f_contents)];//echo $line;

First off, is this the code I'd want to use? Secondly, if my .txt file is located in say following place, how can alter the above code to call the text of the document located there?


I am familiar with the following code, but I just don't know how to incorporate it with the above code:


Am I on the right track here? And, if so, how can I incorporate this in to the first code I listed? Thanks in advance for your help!

Edited by Hopeful
Link to comment
Share on other sites

If your server supports it then you can replace the filename with the URL. Not all servers will support opening remote files. The file function will read the contents of the file into an array, with one line per array element. You can use the shuffle function to randomize the order of array elements. http://www.php.net/manual/en/function.file.phphttp://www.php.net/manual/en/function.shuffle.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...