phphtmlterminal-coloransi-escape

Converting ANSI escape sequences to HTML using PHP


This is a similar question to this one. I would like to convert ANSI escape sequences, especially for color, into HTML. However, I would like to accomplish this using PHP. Are there any libraries or example code out there that do this? If not, anything that can get me part way to a custom solution?


Solution

  • I don't know of any such library in PHP. But if you have a consistent input with limited colors, you can accomplish it using a simple str_replace():

    $dictionary = array(
        'ESC[01;34' => '<span style="color:blue">',
        'ESC[01;31' => '<span style="color:red">',
        'ESC[00m'   => '</span>' ,
    );
    $htmlString = str_replace(array_keys($dictionary), $dictionary, $shellString);