尝试使用 argv 变量和 getopt() 似乎不起作用。除了使用 all - 或 -- 选项之外,任何人都知道解决方法:
<?php
$arr[] = "test:";
$options = getopt(NULL, $arr);
echo $options["test"];
?>
上面的简单示例,当我运行时:
php test.php --test="Hello World"
世界你好
php test.php argv --test="Hello World"
没有输出,因为我放置的值前面没有 - 或 -- 。
最佳答案
function get_opt() {
$options = array();
foreach( $_SERVER[ "argv" ] as $key => $arg ) {
if ( preg_match( '@\-\-(.+)=(.+)@', $arg, $matches ) ) {
$key = $matches[ 1 ];
$value = $matches[ 2 ];
$options[ $key ] = $value;
} else if ( preg_match( "@\-(.)(.)@", $arg, $matches ) ) {
$key = $matches[ 1 ];
$value = $matches[ 2 ];
$options[ $key ] = $value;
}
}
return $options;
}
关于PHP CLI : using argv and getopt not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15960576/