Skip to content

Instantly share code, notes, and snippets.

@sy-records
Created January 2, 2020 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sy-records/e7aee77d15de2c677176e888766008b2 to your computer and use it in GitHub Desktop.
Save sy-records/e7aee77d15de2c677176e888766008b2 to your computer and use it in GitHub Desktop.
PHP遍历命令行参数
function getCommands()
{
$argv = $GLOBALS['argv'];
$tmp = [];
foreach ($argv as $key => $item) {
$name = $item;
$value = '';
if (strpos($name, '=') !== false) {
list($name) = explode('=', $item);
$value = ltrim(strstr($item, "="), "=");
}
if (substr($name, 0, 2) == '--' || substr($name, 0, 1) == '-') {
// 无值参数处理
if (substr($name, 0, 1) == '-' && $value === '' && isset($argv[$key + 1]) && substr($argv[$key + 1], 0, 1) != '-') {
$next = $argv[$key + 1];
if (preg_match('/^[\S\s]+$/i', $next)) {
$value = $next;
}
}
} else {
$name = '';
}
if ($name !== '') {
$tmp[$name] = $value;
}
}
return $tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment