Tag Archives: Generator

PHP: Stream a file line-by-line using a generator

This function will “stream” a file line-by-line, as a Generator.

Can be very useful if for example you need to process a big file, don’t want to read the whole thing into memory, but only process each line by itself.

function read($file)
{
    $fp = fopen($file, 'rb');

    while(($line = fgets($fp)) !== false)
        yield rtrim($line, "\r\n");

    fclose($fp);
}

// Usage
foreach(read('http://example.com') as $line)
{
    var_dump($line);
}

Java: Readable password generator

Needed something to generate random passwords and found Java Password Generator. It generates passwords which are gibberish, but still sort of readable, which I think is a lot nicer than pure gibberish.

I just wanted a simple ‘get me a new random password method’ though, and not all the applet and ‘make several passwords’ stuff. Decided to extract the useful parts and clean it up a bit. In case I need it again, or others need it, and since he states “share your source with others freely”, here is the result 🙂

Continue reading Java: Readable password generator