diff --git a/library/std/exec.php b/library/std/exec.php index 5497357e182..afaab5d88b2 100644 --- a/library/std/exec.php +++ b/library/std/exec.php @@ -4,13 +4,20 @@ function swoole_exec(string $command, &$output = null, &$returnVar = null) $result = Swoole\Coroutine::exec($command); if ($result) { $outputList = explode(PHP_EOL, $result['output']); + foreach ($outputList as &$value) { + $value = rtrim($value); + } + if ('' === ($endLine = end($outputList))) { + array_pop($outputList); + $endLine = end($outputList); + } if ($output) { $output = array_merge($output, $outputList); } else { $output = $outputList; } $returnVar = $result['code']; - return end($outputList); + return $endLine; } else { return false; } diff --git a/php_swoole_library.h b/php_swoole_library.h index 506ebfb8e59..8c00ef0cd41 100644 --- a/php_swoole_library.h +++ b/php_swoole_library.h @@ -17,13 +17,20 @@ static const char* swoole_library_source_std_exec = " $result = Swoole\\Coroutine::exec($command);\n" " if ($result) {\n" " $outputList = explode(PHP_EOL, $result['output']);\n" + " foreach ($outputList as &$value) {\n" + " $value = rtrim($value);\n" + " }\n" + " if ('' === ($endLine = end($outputList))) {\n" + " array_pop($outputList);\n" + " $endLine = end($outputList);\n" + " }\n" " if ($output) {\n" " $output = array_merge($output, $outputList);\n" " } else {\n" " $output = $outputList;\n" " }\n" " $returnVar = $result['code'];\n" - " return end($outputList);\n" + " return $endLine;\n" " } else {\n" " return false;\n" " }\n" diff --git a/tests/swoole_library/exec/exec/2.phpt b/tests/swoole_library/exec/exec/2.phpt new file mode 100644 index 00000000000..c90658bc38e --- /dev/null +++ b/tests/swoole_library/exec/exec/2.phpt @@ -0,0 +1,23 @@ +--TEST-- +swoole_library/exec/exec: Fix $output result inconsistency +--SKIPIF-- + +--FILE-- + +--EXPECT--