[resolved] Code for calling and randomizing contents of a .txt? (4 posts)

  1. HopefulGJL
    Posted 4 years 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)];
    //echo $line;

    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!

  2. s_ha_dum
    Posted 4 years ago #

    file should work. The docs have an example for reading from a web address.

    You could also use fopen or curl.

  3. HopefulGJL
    Posted 3 years 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);
    	echo $quote[array_rand($quote)];

    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.

  4. HopefulGJL
    Posted 3 years ago #

    If anyone is looking to alter this slightly for height around the quotes, or padding, etc., please review this thread here.

Topic Closed

This topic has been closed to new replies.

About this Topic