Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
128 Chapter 5 Implementing with PHP: Standalone Scripts if($i++ == 10) { break; } } print “\n\n”; } ? The script works by reading in a logfile on STDIN and matching each line against $regex to extract individual fields.The script then computes summary statistics, counting the number of requests per unique IP address and per unique Web server user agent. Because combined-format logfiles are large, you can output a . to stderr every 1,000 lines to reflect the parsing progress. If the output of the script is redirected to a file, the end report will appear in the file, but the .’s will. | 128 Chapter 5 Implementing with PHP Standalone Scripts if i 10 break print 11 n n The script works by reading in a logfile on STDIN and matching each line against regex to extract individual fields. The script then computes summary statistics counting the number of requests per unique IP address and per unique Web server user agent. Because combined-format logfiles are large you can output a . to stderr every 1 000 lines to reflect the parsing progress. If the output of the script is redirected to a file the end report will appear in the file but the . s will only appear on the user s screen. Parsing Command-Line Arguments When you are running a PHP script on the command line you obviously can t pass arguments via _GET and _POST variables the CLI has no concept of these Web protocols . Instead you pass in arguments on the command line. Command-line arguments can be read in raw from the argv autoglobal. The following script usr bin env php php print_r argv when run as this . dump_argv.php foo bar barbara gives the following output Array 0 dump_argv.php 1 foo 2 bar 3 barbara Notice that argv 0 is the name of the running script. Taking configuration directly from argv can be frustrating because it requires you to put your options in a specific order. A more robust option than parsing options by hand is to use PEAR s Console_Getopt package. Console_Getopt provides an easy interface to use to break up command-line options into an easy-to-manage array. In addition to Parsing Command-Line Arguments 129 simple parsing Console_Getopt handles both long and short options and provides basic validation to ensure that the options passed are in the correct format. Console_Getopt works by being given format strings for the arguments you expect. Two forms of options can be passed short options and long options. Short options are single-letter options with optional data. The format specifier for the short options is a string of allowed tokens. Option letters can be followed with a .