PHP Random lines from flat file

Discussion in 'PHP' started by chrisjholmes, Feb 17, 2007.

  1. #1
    Hey,

    I hope you can help!!

    I have a text file (as below), and id like to lines of the text file completely at random. I.e. it could show, line 6, line 9, line 1, line 17 etc etc

    TextFile Example:
    Line 1
    Line 2
    Line 3
    Line 4
    Line 5
    Line 6
    Line 7
    Line 8
    Line 9
    Line 10
    Line 11
    Line 12
    Line 13
    Line 14
    Line 15
    Line 16
    Line 17
    Line 18
    Line 19
    Line 20

    i.e. if it was mysql, i could use the following:

    SELECT * FROM table ORDER BY RAND() LIMIT 10

    Hope you can help!

    Chris
     
    chrisjholmes, Feb 17, 2007 IP
  2. infernaliuns

    infernaliuns Active Member

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #2
    Hello Chris

    Well, try this...

    
    
    $file = "path_file.txt";
    $rows = file($file);
    
    //random lines.
    shuffle($rows);
    
    foreach($rows as $r)
    {
          echo $r . "<BR>";
    }
    
    
    PHP:
     
    infernaliuns, Feb 17, 2007 IP
  3. chrisjholmes

    chrisjholmes Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That will display all at random, not limit it to 10?
     
    chrisjholmes, Feb 17, 2007 IP
  4. infernaliuns

    infernaliuns Active Member

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #4
    Oh yeah

    
    $file = "path_file.txt";
    $rows = file($file);
    
    $i = 0;
    
    //random lines.
    shuffle($rows);
    foreach($rows as $r){ 
         if($i == $limit){ break(1); }
         echo $r . "<BR>";
    }
    
    
    PHP:
     
    infernaliuns, Feb 17, 2007 IP
  5. infernaliuns

    infernaliuns Active Member

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    Oh yeah

    
    
    <?
    
    $file = "your_file.txt";
    $rows = file($file);
    
    //Choose a limit of rows.
    $limit = 10;
    $i = 0;
    
    //random lines.
    shuffle($rows);
    foreach($rows as $r){ 
         if($i == $limit){ break(1); }
         echo $r . "<BR>";
         $i++;
    }
    
    ?>
    
    
    PHP:
    or, using for statement...

    
    <?
    
    $file = "your_file.txt";
    $rows = file($file);
    //Choose a limit of rows.
    $limit = 10;
    
    //random lines.
    shuffle($rows);
    for($x = 0; $x < $limit; $x++)
    {
    	echo $rows[$x] . "<BR>";
    }
    
    ?>
    
    PHP:
     
    infernaliuns, Feb 17, 2007 IP
  6. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Or using array_slice:
    $file = "path_file.txt";
    $rows = file($file);
    
    //random lines.
    shuffle($rows);
    // get only the first 10
    $output = array_slice($rows, 0, 10);
    
    foreach($output as $r)
    {
          echo $r . "<BR>";
    }
    
    PHP:
    http://php.net/array_slice
    :)
     
    picouli, Feb 18, 2007 IP
  7. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #7
    More easiest will be

    
    $lines = file("yourfile.txt");
    $rand = rand(1, count($lines));
    
    print $lines[$rand];
    PHP:
    Not tested, but shud work :D
     
    designcode, Feb 18, 2007 IP
  8. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #8
    Wouldn't rand() present to possibility of duplicates?
     
    noppid, Feb 18, 2007 IP
  9. brendandonhue

    brendandonhue Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    http://php.net/file
    http://php.net/array_rand
     
    brendandonhue, Feb 18, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    EDIT:

    Nevermind... I should read... then reply. :)
     
    nico_swd, Feb 18, 2007 IP