Exemplo n.º 1
0
<?php

$line_number = 0;
foreach (FileLineGenerator('sayings.txt') as $line) {
    $line_number++;
    if (mt_rand(0, $line_number - 1) == 0) {
        $selected = $line;
    }
}
print $selected . "\n";
Exemplo n.º 2
0
<?php

function FileLineGenerator($file)
{
    if (!($fh = fopen($file, 'r'))) {
        return;
    }
    while (false !== ($line = fgets($fh))) {
        (yield $line);
    }
    fclose($fh);
}
$file = FileLineGenerator('log.txt');
foreach ($file as $line) {
    if (preg_match('/^rasmus: /', $line)) {
        print $line;
    }
}