This article to introduce you to PHP files randomly take out a data instance, I hope this tutorial will help you to the reunion.
| The code is as follows |
Copy Code |
The first method: $line = getrandline1 (' test.txt '); function Getrandline1 ($filename) { $linenum = 0; $fh = fopen ($filename, ' R '); while (!feof ($FH)) { if ($rowcontents = fgets ($FH)) { $linenum + +; $contens [] = $rowcontents; } } $randline = Mt_rand (0, $linenum-1); $line = $contens [$randline]; Fclose ($FH); return $line; } The second method: $line = Getrandline2 (' test.txt '); function Getrandline2 ($filename) { $contents = File (' Test.txt '); $linenum = count ($contents); $randline = Mt_rand (0, $linenum-1); $line = $contents [$randline]; return $line; } The third method: $line = Getrandline3 (' test.txt '); function Getrandline3 ($filename) { $contents = File (' Test.txt '); Shuffle ($contents); return $contents [0]; } The fourth method: $line = getrandline4 (' test.txt '); function Getrandline4 ($filename) { $linenum = 0; $fh = fopen ($filename, ' R '); while (!feof ($FH)) { if ($linecontents = fgets ($FH)) { $linenum + +; $randint = (Mt_rand (1, 1000000 * $linenum)-1)/1000000); if ($randint < 1) { $line = $linecontents; } } } Fclose ($FH); return $line; } ?> |
http://www.bkjia.com/PHPjc/633151.html www.bkjia.com true http://www.bkjia.com/PHPjc/633151.html techarticle This article to introduce you to PHP files randomly take out a data instance, I hope this tutorial will help you to the reunion. Code to copy code as follows? PHP//The first method: $lin ...