VB icon

Format VB Code

Email
Submitted on: 1/1/2015 10:21:00 AM
By: Kristopher (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: PHP 4.0
Views: 1482
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Format VB Code
// Description: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.
// By: Kristopher (from psc cd)
//
// Inputs:Input is Visual Basic source code.
//
// Returns:Returns color-coded source code in HTML format.
//**************************************

<?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>';
}
?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.