This class show a code formated.
Allow options for to format.
Options: highlight code and to show line number
<?php
class Code
{
function printCode($code, $high_light = 0, $lines_number = 0)
{
if (!is_array($code)) $code = explode("/n", $code);
$count_lines = count($code);
foreach ($code as $line => $code_line) {
if ($lines_number) $r1 = "<span class=/"lines_number/">".($line + 1)." </span>";
if ($high_light) {
if (ereg("</?(php)?[^[:graph:]]", $code_line)) {
$r2 = highlight_string($code_line, 1)."<br />";
} else {
$r2 = ereg_replace("(</?php )+", "", highlight_string("<?php ".$code_line, 1))."<br />";
}
} else {
$r2 = (!$line) ? "<pre>" : "";
$r2 .= htmlentities($code_line);
$r2 .= ($line == ($count_lines - 1)) ? "<br /></pre>" : "";
}
$r .= $r1.$r2;
}
echo "<div class=/"code/">".$r."</div>";
}
}
?>
在一行内使用highlight_string函数
最新推荐文章于 2021-04-25 11:40:12 发布