Advertisement
6_2008-2009 Debugging and Error Handling #200117

Format VB Code

This PHP function takes Visual Basic source code as input, and then outputs a color-coded version of the source code in HTML. Great for sites that have code archives. Simply pull the code from your file or SQL database, run it through this function, and you have your easy to read, color-coded VB code in a second.

AI

Tóm tắt bởi AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Mã nguồn
original-source
<?php
function format($code) {
$reserved = Array("Public", "Private", "Sub", "Dim", "As", "Integer", "End", "Me", "String", "Long", "Function", "Declare", "Lib", "ByVal", "With", "If", "Then", "Else", "Option", "Explicit", "Type", "Const", "Open", "Close", "Print", "Write", "As", "For", "Next", "To");
$numr = count($reserved);
$code = str_replace('(', ' ( ', $code);
$code = str_replace(')', ' ) ', $code);
$code = str_replace('.', ' . ', $code);
$code = str_replace("'", " ' ", $code);
$code = str_replace('"', ' " ', $code);
$code = str_replace(",", " , ", $code);
$lines = explode("\n", $code);
$numl = count($lines);
//for each line
for ($i = 0; $i < $numl; $i++) {
	$lines[$i] = str_replace("\r", '', $lines[$i]);
 $words = explode(' ', $lines[$i]);
 $numw = count($words);
 $line = '';
 //for each word
 for ($j = 0; $j < $numw; $j++) {
  $b = 0;
  //if it's a comment '
  if($words[$j] == "'") {
   $line = substr($line, 0, strlen($line) -1) . '<font color="#008800">' . "'";
   for ($m = $j + 1; $m < $numw; $m++) {
    $line = $line . $words[$m] . ' ';
   }
   $line = $line . "</font>";
   break;
  }
  //if it's a quote "
  if ($words[$j] == '"') {
   $line = substr($line, 0, strlen($line) -1) . $words[$j];
   if ($skip == 1) {
    $skip = 0;
   } else {
    $skip = 1;
   }
  } else {
   if ($skip == 0) { //if we're not skipping
   //for each reserved word
   for ($k = 0; $k < $numr; $k++) {
    if (strtolower(trim($words[$j])) == strtolower($reserved[$k])) {
     $b = 1;
     $line = $line . '<font color="#000088">' . $words[$j] . '</font> ';
     break;
    }
   }
   //if it's not in a comment
   if ($b != 1) {
    $line = $line . $words[$j];
    if(trim($words[$j]) != '"') {
     if(trim($words[$j]) != "'") {
      $line = $line . ' ';
     }
    }
   }
   } else {
    if (trim($words[$j]) == '"') {
     $line = trim($line) . '"';
    } else {
     $line = $line . $words[$j] . ' ';
    }
   }
  }
 }
 $fcode = $fcode . $line . "\n";
}
$fcode = str_replace(" ( ", "(", $fcode);
$fcode = str_replace(" ) ", ")", $fcode);
$fcode = str_replace(" . ", ".", $fcode);
$fcode = str_replace(" ' ", "'", $fcode);
$fcode = str_replace(" , ", ",", $fcode);
return '<pre style="background:#F4F4F4;">' . trim($fcode, "\n") . '</pre>';
}
?>
Upload
Upload
Bình luận gốc (3)
Được khôi phục từ Wayback Machine