Advanced PHP Programming- P4
Số trang: 50
Loại file: pdf
Dung lượng: 506.78 KB
Lượt xem: 9
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
128Chapter 5 Implementing with PHP: Standalone Scriptsif($i++ == 10) { break; } } print “
”; } ?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...
Nội dung trích xuất từ tài liệu:
Advanced PHP Programming- P4128 Chapter 5 Implementing with PHP: Standalone Scripts if($i++ == 10) { break; } } print “ ”; } ?> 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 proto- cols). 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 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 129simple parsing, Console_Getopt handles both long and short options and provides basicvalidation 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 theshort options is a string of allowed tokens. Option letters can be followed with a single :to indicate that the option requires a parameter or with a double :: to indicate that theparameter is optional. Long options are an array of full-word options (for example, --help).The optionstrings can be followed by a single = to indicate that the option takes a parameter or by adouble == if the parameter is optional. For example, for a script to accept the -h and --help flags with no options, and forthe --file option with a mandatory parameter, you would use the following code:require_once “Console/Getopt.php”;$shortoptions = “h”;$longoptons = array(“file=”, “help”);$con = new Console_Getopt;$args = Console_Getopt::readPHPArgv();$ret = $con->getopt($args, $shortoptions, $longoptions);The return value of getopt() is an array containing a two-dimensional array.The firstinner array contains the short option arguments, and the second contains the longoption arguments. Console_Getopt::readPHPARGV() is a cross-configuration way ofbringing in $argv (for instance, if you have register_argc_argv set to off in yourphp.ini file). I find the normal output of getopt() to be a bit obtuse. I prefer to have my optionspresented as a single associative array of key/value pairs, with the option symbol as thekey and the option value as the array value.The following block of code usesConsole_Getopt to achieve this effect:function getOptions($default_opt, $shortoptions, $longoptions){ require_once “Console/Getopt.php”; $con = new Console_Getopt; $args = Console_Getopt::readPHPArgv(); $ret = $con->getopt($args, $shortoptions, $longoptions); $opts = array(); foreach($ret[0] as $arr) { $rhs = ($arr[1] !== null)?$arr[1]:true; if(array_key_exists($arr[0], $opts)) { if(is_array($opts[$arr[0]])) { $opts[$arr[0]][] = $rhs; }130 Chapter 5 Implementing with PHP: Standalone Scripts else { $opts[$arr[0]] = array($opts[$arr[0]], $rhs); } } else { $opts[$arr[0]] = $rhs; } } if(is_array($default_opt)) { foreach ($default_opt as $k => $v) { if(!array_key_exists($k, $opts)) { $opts[$k] = $v; } } } return $opts; } If an argument flag is passed multiple times, the value for that flag will be an array of all the values set, and if a flag is passed without an argument, it is assigned the Boolean ...
Nội dung trích xuất từ tài liệu:
Advanced PHP Programming- P4128 Chapter 5 Implementing with PHP: Standalone Scripts if($i++ == 10) { break; } } print “ ”; } ?> 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 proto- cols). 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 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 129simple parsing, Console_Getopt handles both long and short options and provides basicvalidation 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 theshort options is a string of allowed tokens. Option letters can be followed with a single :to indicate that the option requires a parameter or with a double :: to indicate that theparameter is optional. Long options are an array of full-word options (for example, --help).The optionstrings can be followed by a single = to indicate that the option takes a parameter or by adouble == if the parameter is optional. For example, for a script to accept the -h and --help flags with no options, and forthe --file option with a mandatory parameter, you would use the following code:require_once “Console/Getopt.php”;$shortoptions = “h”;$longoptons = array(“file=”, “help”);$con = new Console_Getopt;$args = Console_Getopt::readPHPArgv();$ret = $con->getopt($args, $shortoptions, $longoptions);The return value of getopt() is an array containing a two-dimensional array.The firstinner array contains the short option arguments, and the second contains the longoption arguments. Console_Getopt::readPHPARGV() is a cross-configuration way ofbringing in $argv (for instance, if you have register_argc_argv set to off in yourphp.ini file). I find the normal output of getopt() to be a bit obtuse. I prefer to have my optionspresented as a single associative array of key/value pairs, with the option symbol as thekey and the option value as the array value.The following block of code usesConsole_Getopt to achieve this effect:function getOptions($default_opt, $shortoptions, $longoptions){ require_once “Console/Getopt.php”; $con = new Console_Getopt; $args = Console_Getopt::readPHPArgv(); $ret = $con->getopt($args, $shortoptions, $longoptions); $opts = array(); foreach($ret[0] as $arr) { $rhs = ($arr[1] !== null)?$arr[1]:true; if(array_key_exists($arr[0], $opts)) { if(is_array($opts[$arr[0]])) { $opts[$arr[0]][] = $rhs; }130 Chapter 5 Implementing with PHP: Standalone Scripts else { $opts[$arr[0]] = array($opts[$arr[0]], $rhs); } } else { $opts[$arr[0]] = $rhs; } } if(is_array($default_opt)) { foreach ($default_opt as $k => $v) { if(!array_key_exists($k, $opts)) { $opts[$k] = $v; } } } return $opts; } If an argument flag is passed multiple times, the value for that flag will be an array of all the values set, and if a flag is passed without an argument, it is assigned the Boolean ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkTài liệu liên quan:
-
52 trang 434 1 0
-
24 trang 359 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 320 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 311 0 0 -
74 trang 303 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0