2

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?

2
  • are you able to control how the output is sent from bash_script.sh? Commented Mar 22, 2017 at 10:28
  • @Dharma I can modify bash_script.sh but not its formatted output since it makes use of another tool which provides the colors Commented Mar 22, 2017 at 10:30

1 Answer 1

1

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:

  1. Define translations
    $colors = ['[01;32m' => '<span style="color:green">', …, '[0m' => '</span>']

  2. 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.

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

2 Comments

I think this could work in case of I have the output with the color codes (as in the example I put with ls --color command), however unfortunately my real script does not provide these color codes, so I won't be able to translate them :(
Just tried now, it works fine for the ls --color example but it doesn't work with my real script, I don't understand why because when I run it from CLI I get properly the html color codes. Since my bash_script.sh is actually an expect script, I'm afraid that it is not able to handle the colors properly...

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.