Last active
August 20, 2016 19:30
-
-
Save mulatinho/04e451855a04b0aa3bad to your computer and use it in GitHub Desktop.
simple function to read code and print in pretty format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function mlt_valida_input($input) { | |
$pattern = '[^A-Za-z0-9.]'; | |
return ereg($pattern, $input); | |
} | |
function mlt_read_code($input) { | |
if (mlt_valida_input($input)) | |
return 1; | |
if (!($fp = fopen($input, "r"))) | |
return 1; | |
print "<br/>"; | |
print "<span style='display:block; background: #e0e0f0; width: 550px; font-size: 10px;'>.:.:. init source code: '$input'</span>"; | |
$lines = 0; | |
$pattern = "/(if |while | do |elif |^fi|else |then|while |do |return)/"; | |
while (($buffer = fgets($fp, 4096))) { | |
$buffer[strlen($buffer)-1] = ' '; | |
$buffer = preg_replace('/\t/', ' ', $buffer); | |
$buffer = preg_replace('/</', '<', $buffer); | |
$buffer = preg_replace("$pattern", '<b>\\1</b>', $buffer); | |
if (!($lines % 2)) | |
$bg = "#e0e0e0"; | |
else | |
$bg = "#f0f0f0"; | |
printf("<span style='display: block; background: %s; width: 550px; font-size: 10px;'>%03d . %s</span>\n", | |
$bg, $lines, $buffer); | |
$lines++; | |
} | |
print "<span style='display:block; background: #e0e0f0; width: 550px; font-size: 10px;'>.::.::. end source code: '$input'</span>"; | |
print "<br/>"; | |
fclose($fp); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment