A library to convert a text with ANSI codes to HTML

v3.0.0 2022-06-07 10:39 UTC

This package is auto-updated.

Last update: 2022-09-07 11:13:53 UTC


Build Status Coverage Status

ANSI to HTML5 Converter for PHP

*** this is only a fork of "https://github.com/sensiolabs/ansi-to-html" ***

This small library only does one thing: converting a text containing ANSI codes to an HTML5 fragment:

require_once __DIR__ . '/vendor/autoload.php';

use voku\AnsiConverter\AnsiToHtmlConverter;

$converter = new AnsiToHtmlConverter();

$html = $converter->convert($ansi);

The $ansi variable should contain a text with ANSI codes, and $html will contain the converted HTML5 version of it.

You can then output the HTML5 fragment in any HTML document:

        <pre style="background-color: black; overflow: auto; padding: 10px 15px; font-family: monospace;"
        ><?php echo $html ?></pre>

The converter supports different color themes:

use voku\AnsiConverter\Theme\SolarizedTheme;

$theme = new SolarizedTheme();
$converter = new AnsiToHtmlConverter($theme);

By default, the colors are inlined into the HTML, but you can also use classes by turning off style inlining:

$converter = new AnsiToHtmlConverter($theme, false);

And the asCss() method of the theme object lets you retrieve the theme styles as a CSS snippet:

$styles = $theme->asCss();

which you can then use in your HTML document:

            <?php echo $styles ?>

            .ansi_box { overflow: auto; padding: 10px 15px; font-family: monospace; }
        <pre class="ansi_color_bg_black ansi_color_fg_white ansi_box"><?php echo $html ?></pre>

You can also override the theme by calling the setTheme() method with a new theme.