phpshellsyntaxhighlighterpassthru

How can I syntax highlight the output from the exec() function in PHP?


I'm fetching output from the exec() function and I would like to have some syntax highlighting in the results.

Raw output

Current output is raw:

* [35mmanu[m/etc/init.d/mast: line 105: /var/log/mast/mast-all.log: Permission denied 

Text such as [35m are color highlighting in shell context.

Goal

I want to do it in HTML, I already have a colored shell script.

highlighted syntax


Solution

  • The question A library to convert ANSI escapes (terminal formatting/color codes) to HTML has an answer to this question.

    Solution

    aha is a Ansi to HTML Adapter written in C. It's available in an Ubuntu package and on github theZiz/aha.

    My code is then simply:

       exec("$command | aha", $output, $exitCode);
       foreach($output as $k => $line) {
           if ($line == '1') { continue; }
           echo "$line";
       }
    

    Description

       aha takes SGR-colored Input and prints W3C conform HTML-Code.
       aha reads the Input from a file or stdin and writes HTML-Code to stdout.
    

    There is some nice options: