function rand_line($fileName, $maxLineLength = 4096) {
$handle = @fopen($fileName, "r");
if ($handle) {
$random_line = null;
$line = null;
$count = 0;
while (($line = fgets($handle, $maxLineLength)) !== false) {
$count++;
// P(1/$count) probability of picking current line as random line
if(rand() % $count == 0) {
$random_line = $line;
}
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
fclose($handle);
return null;
} else {
fclose($handle);
}
return $random_line;
}
}
// usage
echo rand_line("daftar-kata-inggris.txt");
echo "<br/><br/>".rand_line("daftar-kata-indonesia.txt");
Smoga bermanfaat....
ConversionConversion EmoticonEmoticon