#native_company# #native_desc#
#native_cta#

Colorizator: Convert php code ->colorful HTML page

By Alexander Yanuar Koentjara
on March 14, 2001

Colorizator v1.0

Hello ! I want to introduce you “Colorizator v1.0”, this tool can convert your php code into colorful HTML page :
1. It will redd-ish all reserved words
2. It will green-ish all variables
3. It will blood-ish all strings
4. It will grey-ish all comments
5. It will blu-ish all declaration
6. All other PHP code become Naaaaavy !!!
7. All other HTML code become black …

You can customize the color, add necessary reserved word or declaration statements.

The result can be seen at http://www.geocities.com/lexzeus/colorizator.htm … I use Colorizator to convert colorizator.php code into html 😛




Here comes the code : file : colorizator.php ********************** <? /* Program name : Colorizator v1.0 Author : Alexander Yanuar Koentjara [email protected] Usage : php -q colorizator.php source_code.php output.htm Purpose : To make php code become a colorful HTML page !! */ if (!trim($argv[2])) print "Need <param1> and <param2n". "Usage : php -q colorizator.php source_code.php output.htm"; $CODE_BEGIN = "<"."?"; // sorry ... so it will not caught by Colorizator $CODE_END = "?".">"; // sorry ... so it will not caught by Colorizator $arr_RESERVED_WORD = array( "if", "while", "for", "return", "else"); $arr_DECLARATION_WORD = array( "class", "var", "function", "global", "GLOBAL"); $color_RESERVED_WORD = "RED"; $color_DECLARATION_WORD = "BLUE"; $color_COMMENT = "#707090"; $color_DEFAULT = "#3333aa"; $color_STRING = "#800000"; $color_VARIABLE = "GREEN"; $color_HTML = "BLACK"; $splitter = "|X*-(LexZ)-*X|"; $splitter2 = "|X*-(Kick)-*X|"; function generate_ereg($arr) { $x=""; for($i=0;$i<count($arr);$i++) $x.="|$arr[$i]"; return substr($x,1); } $RESERVED_WORD = generate_ereg($arr_RESERVED_WORD); $DECLARATION_WORD = generate_ereg($arr_DECLARATION_WORD); $file_from = trim($argv[1]); $file_to = trim($argv[2]); $f_arr = join("",file($file_from)); $fr = fopen($file_to,"w"); fputs($fr, "<HTML><BODY BGCOLOR=WHITE> <!-- This php code is converted into HTML using : Colorizator v1.0 (c) 2001 by Alexander Yanuar Koentjara ([email protected]) -->n"); fputs($fr,"
<FONT FACE='Courier New' size=2 color=$color_DEFAULT>");

$delimiter=array(0=>0,
                 "//"=>array(2,"[rn]",$color_COMMENT),
                 """=>array(1,""",$color_STRING),
                 "'"=>array(1,"'",$color_STRING),
                 "/*"=>array(2,"*/",$color_COMMENT),
                 "#"=>array(1,"[rn]",$color_COMMENT));

function x_parse($fr, $str)
{
  GLOBAL $delimiter, $splitter, $splitter2, $color_VARIABLE,
         $RESERVED_WORD, $DECLARATION_WORD,
         $color_RESERVED_WORD,
         $color_DECLARATION_WORD;
  $flag = 0;
  $pos2 = -1;
  $all = "";

  while($pos2<strlen($str)-1)
    {
    $pos2++;
    if ($flag)
       {
       $flag2=1;
       if (substr($str,$pos2-1,1)=="") $flag2=0;
       if (substr($str,$pos2-2,2)=="\") $flag2=1;

       if (ereg($delimiter[$flag][1],substr($str,$pos2,$delimiter[$flag][0]))
           && $flag2)
          {
          $all.=htmlspecialchars(substr($str,$pos2,1)).
                "$splitter2</FONT>";
          $flag=0;
          }
       else
          {
          $all.=htmlspecialchars(substr($str,$pos2,1));
          }
       }
    else
       {
       reset($delimiter);
       while (list($key,$val)=each($delimiter))
             {
             if ($key)
                {
                if (substr($str,$pos2,$val[0])==$key)
                   {
                   $all.="<FONT COLOR=$val[2]>$splitter".
                         htmlspecialchars(substr($str,$pos2,$val[0]));
                   $pos2+=($val[0]-1);
                   $flag=$key;
                   break;
                   }
                }
             }
       if (!$flag)
          $all.=htmlspecialchars(substr($str,$pos2,1));
       }
    }

  $all=eregi_replace("($[a-z0-9_]+)",
                     "<FONT COLOR=$color_VARIABLE>1</FONT>",
                      $all);

  $all=ereg_replace("([rn t{};])($RESERVED_WORD)([rn t{};])",
                    "1<FONT COLOR=$color_RESERVED_WORD>2</FONT>3",
                    $all);

  $all=ereg_replace("([rn t{};])($DECLARATION_WORD)([rn t{};])",
                    "1<I><FONT COLOR=$color_DECLARATION_WORD>2</FONT></I>3",
                    $all);

  $f_arr = explode($splitter,$all);
  $all = "";

  if (count($f_arr)>1)
     for ($i=0;$i<count($f_arr);$i++)
         {
         $f_arr2 = explode($splitter2,$f_arr[$i]);
         if (count($f_arr2)>1)
            {
            $f_arr2[0] = eregi_replace(
                  "(<[/]{0,1}FONT[^>]*>|<[/]{0,1}B>|<[/]{0,1}I>)",
                  "",$f_arr2[0]);
            $all.=$f_arr2[0].$f_arr2[1];
            }
         else
            $all.=$f_arr[$i];
         }
   else
      $all = join("",$f_arr);

  fputs($fr,$all);
}

$pos1=0; $pos2=0;

while(1)
  {
  $posx = strpos(" $f_arr",$CODE_BEGIN);
  if (!$posx && strlen($f_arr)>0) $posx=strlen($f_arr)+1;
  if ($posx)
     {
     $posx--;
     fputs($fr,
           "<FONT COLOR=$color_HTML>".
           htmlspecialchars(substr($f_arr,0,$posx)).
           "</FONT>"
           );
     }

  if (!strpos(" $f_arr",$CODE_BEGIN)) break;
  $pos1 = strpos("$f_arr","$CODE_BEGIN")+2;

  $pos2 = strpos($f_arr,"$CODE_END") - 1;
  if ($pos2<1)
     $pos2=strlen($f_arr)-1;

  fputs($fr,"<?");
  $str = substr($f_arr,$pos1,$pos2-$pos1+1);
  x_parse($fr,$str);

  fputs($fr,"?&gt;");
  $f_arr = substr($f_arr,$pos2+3);
  }

fputs($fr,"</FONT>

n");

fputs($fr,"</BODY>n</HTML>n");
fclose($fr);
?>

How to use ?
It is easy, you will have to use PHP4 (I haven’t test it in PHP3) :

shell>php -q colorizator.php source_code.php output.htm

source_code.php is a php source code that you want to convert
output.htm is the result

If you find any bugs, please send me e-mail : [email protected], but so far so good …

Best regards,

Alexander Yanuar Koentjara
SYSTEM DEVELOPER (www.globalsources.com)