Reputation: 2101
I have a php file which currently puts in the browser the output of a bash script:
<?php
ob_implicit_flush(true);
ob_end_flush();
$cmd = "./bash_script.sh";
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo '<pre>';
if (is_resource($process)) {
while ($s = fgets($pipes[1])) {
print $s;
}
}
echo '</pre>';
?>
However, in CLI the output of my bash_script.sh is colored formatted but in the browser output there is no formatting and colors are not visible.
I have tried the following simple example with command ls --color
:
<?php
$cmd = "ls --color";
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo '<pre>';
if (is_resource($process)) {
while ($s = fgets($pipes[1])) {
print $s;
}
}
echo '</pre>';
?>
And its output comes with color codes (or at least I believe so), that is:
[01;34mFolder1[0m
[01;34mFolder2[0m
[01;34mFolder3[0m
[01;32mFile1[0m
[01;34mFolder4[0m
However, with my script, those color codes don't appear.
Is it possible to print the same colored output I get in CLI to the browser?
Upvotes: 2
Views: 1038
Reputation: 828
Since there are color formats in the output, you could set a translations table that converts between the cli and php.
A quick-n-dirty example:
Define translations
$colors = ['[01;32m' => '<span style="color:green">', …, '[0m' => '</span>']
Then replace
str_replace(array_keys($colors), array_values($colors))
NOTE: usually color formats are defined in this form \e[32mHello world
, where \e
is a shortand for ESCAPE char, so see case by case forms of defining a color format.
Tool way: you might also try if this works fine: aha, an Ansi HTML Adapter.
Upvotes: 1