data from a text?

Discussion in 'PHP' started by jonhyhar, Feb 25, 2010.

  1. #1
    guys, how can we pick out random data from a txt file?
     
    jonhyhar, Feb 25, 2010 IP
  2. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want to randomly read a line and print its data you could do something like this:

    
    $file = file('/link/to/text.txt');
    echo $file[rand(0, count($file) - 1)];
    
    PHP:
     
    Nyu, Feb 25, 2010 IP
  3. jonhyhar

    jonhyhar Active Member

    Messages:
    166
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Thank you very much Nyu :)
     
    jonhyhar, Feb 25, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Or you can do:

    <?php
    $file = file("text.txt");
    echo $file[array_rand($file)];
    ?>
    PHP:
     
    danx10, Feb 25, 2010 IP