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 ...
- 09-10-2010 #1
[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:
creates one very long string of the output.Code:print passthru('fortune');
Looks like it should work, but doesn't. Somehow I don't think nl2br() recognizes the newline from the command output.Code:print passthru(nl2br('fortune'));
So now I do:
But this causes me to sacrifice indentation in the source file, otherwise the generated html will have loads of whitespace.Code:print <pre>; print passthru('fortune'); print </pre>;
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
- 09-10-2010 #2
Hi,
is mindless. This would be correct:Code:print passthru(nl2br('fortune'));Code:print nl2br(passthru('fortune'));
- 09-10-2010 #3
Originally Posted by Manko10
But, both do exactly the same thing
And neither do what I want... PHP5.2Can't tell an OS by it's GUI
- 09-10-2010 #4
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 tryPHP Code:print preg_replace('#(\r\n?|\n)#', '<br>$1', passthru('fortune'));
- 09-10-2010 #5Ah, yes, thanks.
Originally Posted by Manko10
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.PHP Code:print preg_replace('#(\r\n?|\n)#', '<br>$1', passthru('fortune'));
I made this php file:
And this text file called 'test':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>";
?>
Code:line 1 line 2
Output in browser:
Output in html source:Code:--- line 1 line 2 --- line 1 line 2 --- line 1 line 2 --- line 1 line 2 ---
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
- 09-10-2010 #6
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().
- 09-10-2010 #7ASCIIPHP Code:
print mb_detect_encoding(passthru('cat test'));
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.
Originally Posted by Manko10 Can't tell an OS by it's GUI
- 09-10-2010 #8
- 09-10-2010 #9Well, that's how the whole thing got started, because exec() just saves the last line of the output.
Originally Posted by Manko10
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:
Just to preserve newlines in html, lolPHP Code:$command = "cat file";
$result = exec_query($command);
while ( $row = exec_fetch_array($result) )
{
print "$row<br>\n";
}
But then I figured this:
Kinda works. And it's a sort of buffering I presume...PHP Code:$output = shell_exec('cat test');
echo "<pre>$output</pre>";
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 05:55 PM.
Can't tell an OS by it's GUI
- 09-10-2010 #10
You can specify the $output parameter as mentioned in the manual (PHP: exec - Manual). Otherwise shell_exec() should also be working for you.


