0

I am looking for something in php that would given output (raw) of a system command in a variable along with the return code.

  • exec does this, but the output is in array and hence the data returned is not proper(as \n comes in new index).
  • system outputs the data in the output stream and not in a variable.
  • shell_exec does not give the return value but gives raw data.
2
  • 2
    Can't you just implode() the result from exec() to turn it into a string? Commented Feb 2, 2012 at 9:14
  • hey that worked...was stuck since a long time for this silly thing....thanks a lot Commented Feb 2, 2012 at 10:04

1 Answer 1

4

Sounds like you're looking for output buffering:

ob_start();
system($command, $returnCode);
$output = ob_get_clean();

This should preserve all white-space characters at the end of each output line (exec as you wrote destroys these, so implode would not be an option).

Alternatively, you can open a process and aquire the pipes (standard output, STDOUT) and read the output out of these. But it's more complicated (but gives you more options). See proc_open.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.