5

i am trying to parse lines from a log and output html and wanted the color codes to work.

i found this class online that should work but it doesn't color anything nor does it remove the control codes. it is supposed to replace the control codes with equivalent html but completely ignores it outputting

[0;35;22m/plugins: [0;37;1mGets a list of plugins running on the server[m

here is the class

<?php

 function bashColortoHtml($string) {
    $ret = false;

    if(!empty($string)) {
        $_colorPattern = array(
            '/\\033\[1;33m(.*?)\\033\[0m/s',
            '/\\033\[0;31m(.*?)\\033\[0m/s',
            '/\\033\[0;34m(.*?)\\033\[0m/s',
            '/\\033\[0;36m(.*?)\\033\[0m/s',
            '/\\033\[0;35m(.*?)\\033\[0m/s',
            '/\\033\[0;33m(.*?)\\033\[0m/s',
            '/\\033\[1;37m(.*?)\\033\[0m/s',
            '/\\033\[0;30m(.*?)\\033\[0m/s',
            '/\\033\[0;32m(.*?)\\033\[0m/s'
        );
        $_colorReplace = array(
            '<span class="yellow">$1</span>',
            '<span class="red">$1</span>',
            '<span class="blue">$1</span>',
            '<span class="cyan">$1</span>',
            '<span class="purple">$1</span>',
            '<span class="brown">$1</span>',
            '<span class="white">$1</span>',
            '<span class="black">$1</span>',
            '<span class="green">$1</span>'
        );

        $ret = preg_replace($_colorPattern, $_colorReplace, $string);
    }

    return $ret;
}
?>
<?php
$output = bashColortoHtml('[0;35;22m/plugins: [0;37;1mGets a list of plugins running on the server[m');
echo $output;
?>

what is the issue with this class and/or is there a better way of doing this with php

2 Answers 2

6

I had to solve the same problem today and wrote this simple and plain function. Be sure to check, if it fits your needs, you might have to add some cases.

//
// Converts Bashoutput to colored HTML
//
function convertBash($code) {
    $dictionary = array(
        '[1;30m' => '<span style="color:black">',
        '[1;31m' => '<span style="color:red">', 
        '[1;32m' => '<span style="color:green">',   
        '[1;33m' => '<span style="color:yellow">',
        '[1;34m' => '<span style="color:blue">',
        '[1;35m' => '<span style="color:purple">',
        '[1;36m' => '<span style="color:cyan">',
        '[1;37m' => '<span style="color:white">',
        '[m'   => '</span>'
    );
    $htmlString = str_replace(array_keys($dictionary), $dictionary, $code);
    return $htmlString;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Combining the previous answers and adding all colors I found it looks something like this:

function bashColorToHtml($string) {
    $colors = [
        '/\[0;30m(.*?)\[0m/s' => '<span class="black">$1</span>',
        '/\[0;31m(.*?)\[0m/s' => '<span class="red">$1</span>',
        '/\[0;32m(.*?)\[0m/s' => '<span class="green">$1</span>',
        '/\[0;33m(.*?)\[0m/s' => '<span class="brown">$1</span>',
        '/\[0;34m(.*?)\[0m/s' => '<span class="blue">$1</span>',
        '/\[0;35m(.*?)\[0m/s' => '<span class="purple">$1</span>',
        '/\[0;36m(.*?)\[0m/s' => '<span class="cyan">$1</span>',
        '/\[0;37m(.*?)\[0m/s' => '<span class="light-gray">$1</span>',

        '/\[1;30m(.*?)\[0m/s' => '<span class="dark-gray">$1</span>',
        '/\[1;31m(.*?)\[0m/s' => '<span class="light-red">$1</span>',
        '/\[1;32m(.*?)\[0m/s' => '<span class="light-green">$1</span>',
        '/\[1;33m(.*?)\[0m/s' => '<span class="yellow">$1</span>',
        '/\[1;34m(.*?)\[0m/s' => '<span class="light-blue">$1</span>',
        '/\[1;35m(.*?)\[0m/s' => '<span class="light-purple">$1</span>',
        '/\[1;36m(.*?)\[0m/s' => '<span class="light-cyan">$1</span>',
        '/\[1;37m(.*?)\[0m/s' => '<span class="white">$1</span>',
    ];

    return preg_replace(array_keys($colors), $colors, $string);
}

To display the colors in the html you have to define the classes in css or use the style attribute.

Comments

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.