Support » Fixing WordPress » Code for calling and randomizing contents of a .txt?
4 years, 8 months ago
I am looking to call the contents of a .txt document and randomize those contents.
I think I found the php code that might work. Would something like this be correct?
$f_contents = file("TheRandomContent.txt");
//$line = $f_contents[array_rand($f_contents)];
If that is correct, then how can I alter it to call the document if it’s located say here:
Am I on the right track here?
And, if so, how can I incorporate this in to the first code I listed? I’m a real beginner, so I’m kindly asking for specifics, please.
Thanks in advance for your help!
file should work. The docs have an example for reading from a web address.
You could also use fopen or curl.
4 years, 7 months ago
I have found my answer here in those post, which led to this site containing the answer.
For ease, I’m re-posting that code here:
$file = "/home/public_html/path/to/random-quotes.txt";
$quote = file($file);
With this solution, you upload your text file to your file manager, and then put your pathway to that text file in place of the one above.
Thanks for the info everyone!
P.S. Even though that code basically has “quote” in it, it also works for other things, like banner ads. Even if it’s not the “best” solution, it is working.
Oh yeah, I also found this solution, but it does not call from a text file though so you’d have to have everything you want to randomize directly in your php file.
If anyone is looking to alter this slightly for height around the quotes, or padding, etc., please review this thread here.