Unix colors to HTML (by PHP)

Hi there,
this is a really short code to enable you to transform colored unix texts into html spans. Can be useful if you need to display something coming from your unix machine without coding it again in php.

All you need before pasting the code is to write this above the function :

  exec ($yourcommand, $input);
  echo doTransform ($input);

Here it goes !

function doTransform ($input) {
  $transform = array(
        1  => '$fontWeight="bold";',
        21 => '$fontWeight="thin";',
        4  => '$textDecoration="underline";',
        24 => '$textDecoration="none";',
        31 => '$color="darkred";',
        32 => '$color="darkgreen";',
        33 => '$color="saddlebrown";',
        34 => '$color="darkblue";',
        35 => '$color="darkmagenta";',
        36 => '$color="darkslateblue";',
        37 => '$color="white";',
        0  => '$fontWeight="thin";$color="black";$backgroundColor="transparent";$textDecoration="none";',
        30 => '$color="black";',
        40 => '$backgroundColor="transparent";',
        41 => '$backgroundColor="darkred";',
        42 => '$backgroundColor="darkgreen";',
        43 => '$backgroundColor="saddlebrown";',
        44 => '$backgroundColor="darkblue";',
        45 => '$backgroundColor="darkmagenta";',
        46 => '$backgroundColor="darkslateblue";',
        47 => '$backgroundColor="white";',
        );

  $fontWeight="thin";
  $textDecoration="none";
  $color="black";
  $backgroundColor="transparent";
  $outputLine = "<span>";

  foreach ($input as $inputLine){
    $inputLine = str_replace (" ", "&nbsp;", $inputLine);
    preg_match_all ("/".chr(27)."\[([0-9]+;)*([0-9]+m)/i", $inputLine, $matches, PREG_OFFSET_CAPTURE);
    $lastIndexOutput = 0;
    $currentMatchNumber = 0;
    foreach ($matches [0] as $match){
      $outputLine .= substr($inputLine, $lastIndexOutput, $match [1] - $lastIndexOutput);
      $outputLine .= "</span><span style=\"";

      for ($replacementIndex = 1 ; $replacementIndex < count ($matches) ; $replacementIndex++){
        if ($matches [$replacementIndex] [$currentMatchNumber] [1] != -1){
          $oneReplacement = $matches [$replacementIndex] [$currentMatchNumber] [0];
          $oneReplacement = substr ($oneReplacement, 0, strlen ($oneReplacement) - 1);
          eval ($transform [$oneReplacement]); 
        }
      }

      $outputLine .= "font-weight:$fontWeight;text-decoration:$textDecoration;color:$color;background-color:$backgroundColor";

      $outputLine .= "\">";
      $lastIndexOutput = $match [1] + strlen ($match [0]);
      $currentMatchNumber++;
    }
    $outputLine .= "<br /></span>"; 
  } 
  return $outputLine;
}

Have fun.