Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
I've got a little webpage that uses PHP's passthru() to display the output of a shell command. Now I don't care much for the formatting, but I want to at ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049

    [SOLVED] PHP - adding <br> in passthru() output


    I've got a little webpage that uses PHP's passthru() to display the output of a shell command. Now I don't care much for the formatting, but I want to at least preserve the newline characters of the output of the command.

    Using `fortune` as an example:

    Just:
    Code:
    print passthru('fortune');
    creates one very long string of the output.
    Code:
    print passthru(nl2br('fortune'));
    Looks like it should work, but doesn't. Somehow I don't think nl2br() recognizes the newline from the command output.

    So now I do:
    Code:
    print <pre>;
    print passthru('fortune');
    print </pre>;
    But this causes me to sacrifice indentation in the source file, otherwise the generated html will have loads of whitespace.

    Any thoughts? I'm not really at home in all the different methods of generating newline characters so perhaps I'm missing something?
    Can't tell an OS by it's GUI

  2. #2
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    Hi,

    Code:
    print passthru(nl2br('fortune'));
    is mindless. This would be correct:
    Code:
    print nl2br(passthru('fortune'));

  3. #3
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    Quote Originally Posted by Manko10
    Hi,

    Code:
    print passthru(nl2br('fortune'));
    is mindless. This would be correct:

    Code:
    print nl2br(passthru('fortune'));

    But, both do exactly the same thing

    And neither do what I want... PHP5.2
    Can't tell an OS by it's GUI

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    No, they don't do the same thing.
    Your version adds BR tags to the string 'fortune' where obviously aren't any and my version adds BR to the output of passthru('fortune'). The latter should definitely work. If not there might be some issue with the break escape sequences not with the function notation itself. If so try
    PHP Code:
    print preg_replace('#(\r\n?|\n)#''<br>$1'passthru('fortune')); 

  6. #5
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    Quote Originally Posted by Manko10
    No, they don't do the same thing.
    Ah, yes, thanks.

    PHP Code:
    print preg_replace('#(\r\n?|\n)#''<br>$1'passthru('fortune')); 
    Nope, but in all instances, always the html source displays correct. So there is are newlines coming from passthru('fortune') and that are showing up in the html source.


    I made this php file:
    PHP Code:
    <?php
    print "<br>---<br>";
    print 
    passthru('cat test');
    print 
    "<br>---<br>";
    print 
    nl2br(passthru('cat test'));
    print 
    "<br>---<br>";
    print 
    passthru(nl2br('cat test'));
    print 
    "<br>---<br>";
    print 
    preg_replace('#(\r\n?|\n)#''<br>$1'passthru('cat test'));
    print 
    "<br>---<br>";
    ?>
    And this text file called 'test':
    Code:
    line 1
    line 2


    Output in browser:
    Code:
    ---
    line 1 line 2
    ---
    line 1 line 2
    ---
    line 1 line 2
    ---
    line 1 line 2
    ---
    Output in html source:
    HTML Code:
    <br>---<br>line 1
    line 2
    <br>---<br>line 1
    line 2
    <br>---<br>line 1
    line 2
    <br>---<br>line 1
    line 2
    <br>---<br>
    Can't tell an OS by it's GUI

  7. #6
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    Oh actually I forgot that passthru() prints its output directly to STDOUT and of course for this reason it's called "passthru". So it's not an issue with the line break. To get BR tags you either have to use output buffering or even better just use nl2br(exec('command')) instead of passthru().

  8. #7
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    PHP Code:
    print mb_detect_encoding(passthru('cat test')); 
    ASCII

    Quote Originally Posted by Manko10
    as well as the character set your PHP script is saved with.
    That should be the same. Both where created using vim. I don't know if it may play a factor, so I created a similar file with another editor (kedit) but still the same.
    Can't tell an OS by it's GUI

  9. #8
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    Just edited the above posting. Forget mb_detect_encoding().

  10. #9
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,049
    Quote Originally Posted by Manko10
    nl2br(exec('command'))
    Well, that's how the whole thing got started, because exec() just saves the last line of the output.

    And I don't really have a picture what you mean by 'buffering' in this case

    If there was some form of exec_fetch_array() like you can do with sql queries, that would be something. But even then it would seem just silly to do:
    PHP Code:
    $command =    "cat file";
    $result =    exec_query($command);

    while        ( 
    $row exec_fetch_array($result) )
    {
        print    
    "$row<br>\n";

    Just to preserve newlines in html, lol



    But then I figured this:
    PHP Code:
    $output shell_exec('cat test');
    echo 
    "<pre>$output</pre>"
    Kinda works. And it's a sort of buffering I presume...
    I don't really like having to use those <pre> tags when all I need is a <br> (or <br /> as it's called nowadays), but if that's the way it is, then that's the way it's gotta be.
    Last edited by Freston; 09-10-2010 at 06:55 PM.
    Can't tell an OS by it's GUI

  11. #10
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    You can specify the $output parameter as mentioned in the manual (PHP: exec - Manual). Otherwise shell_exec() should also be working for you.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •