Skip to content

Repository files navigation

Where to start

Literature and other sources for PHP developers

Feel free to add yours favorite, thanks.

Tools

Feel free to add yours favorite, thanks.


Zadání

Napiště "univerzální" program, který přečte libovolně dlouhý textový soubor. Řádek po řádku bude aplikovat uživatelské filtry a dekorátory. Výstupem programu bude počet stejných (upravených) řádků a jejich četností.

Použijte co nejvíce vlastností moderního PHP. Doporučení:

Bonus

Upravte program tak, aby vypisoval průběžný stav nekonečného streamu.

Řešení

Bude zveřejněno během workshopu ve středu 28.3.2018. Přijďte :-)

Příklad

php old.php example.log

Vstupní soubor

[2018-03-13 12:16:10] test.DEBUG: Test message [] []
[2018-03-13 12:16:10] test.ERROR: Test message [] []
[2018-03-13 12:16:10] test.WARNING: Test message [] []
[2018-03-13 12:16:10] test.WARNING: Test message [] []
[2018-03-13 12:16:10] test.INFO: Test message [] []
[2018-03-13 12:16:10] test.NOTICE: Test message [] []
[2018-03-13 12:16:10] test.EMERGENCY: Test message [] []
[2018-03-13 12:16:10] test.ALERT: Test message [] []
[2018-03-13 12:16:10] test.ERROR: Test message [] []
[2018-03-13 12:16:10] test.NOTICE: Test message [] []

Výstup

error: 2
warning: 2
notice: 2
info: 1
emergency: 1
alert: 1

Implementace ve "starém" PHP

<?php$pattern = '/test\.(\w+)/';
// read and parse file$log = file_get_contents($argv[1]);
$rows = explode(PHP_EOL, $log);
// build stats$stats = array();
foreach ($rowsas$row) {
// decorator: extract log levelif (preg_match($pattern, $row, $matches)) {
$level = strtolower($matches[1]);
// filter: do not accept DEBUGif ($level != 'debug') {
if (array_key_exists($level, $stats)) {
$stats[$level]++;
} else {
$stats[$level] = 1;
}
}
}
}
// show statsarsort($stats);
foreach ($statsas$level => $count) {
echo"$level: $count" . PHP_EOL;
}

About

Zadání a řešení úkolů pro workshop "Moderní PHP"

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages