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函数
最新推荐文章于 2025-07-23 12:40:25 发布
本文介绍了一个PHP类,该类能够对输入的代码进行格式化,并提供选项以实现代码高亮显示及显示行号的功能。通过使用highlight_string函数,可以有效支持多种编程语言的语法高亮。
215

被折叠的 条评论
为什么被折叠?



