3

i have a php file calls a script and prints the output like this

    $output=shell_exec('/usr/bin/python hello.py');
    echo $output;

it prints;

    b'total 16\ndrwx---r-x 2 oae users 4096 Jul 31 14:21 .\ndrwxr-x--x+ 9 oae root 4096 Jul 26 13:59 ..\n-rwx---r-x 1 oae users 90 Aug 3 11:22 hello.py\n-rwx---r-x 1 oae users 225 Aug 3 11:22 index.php\n'

but it should be like this;

    total 16K
    drwx---r-x  2 oae users 4.0K Jul 31 14:21 ./
    drwxr-x--x+ 9 oae root  4.0K Jul 26 13:59 ../
    -rwx---r-x  1 oae users   90 Aug  3 11:22 hello.py*
    -rwx---r-x  1 oae users  225 Aug  3 11:22 index.php*

\n characters shouldn't be shown.How can i solve this?

1
  • ok. Problem was python script. I think i should fix this problem from script. The pre methods are working if i use shell_exec('ls -la ./'). Commented Aug 3, 2012 at 9:47

4 Answers 4

5

this can be work

$output=shell_exec('/usr/bin/python hello.py');
echo "<pre>";
print_r($output);
echo "</pre>";
Sign up to request clarification or add additional context in comments.

Comments

0

Hope you are trying to print this string in web. In that case, PHP may not work with \n characters instead it expects to be <br/> PHP has an inbuilt function which takes care of this transformation of strings.

http://php.net/manual/en/function.nl2br.php

1 Comment

i tried this but it only converts the last newline character to <br/>.
0

An alternative would be wrapping the string between <pre>...</pre> tags.

Comments

0

It seems like an output from a Python 3 script:

from subprocess import check_output as qx
print(qx(['ls']))

The subprocess returns bytes and the print function prints their representation as b'...\n...' where '\n' are two characters (not a newline).

You could fix it by converting qx() returned value to Unicode and then printing it or by writing the bytes as is:

sys.stdout.buffer.write(qx(['ls']))

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.