在一行內使用highlight_string函數

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("(&lt;/?php&nbsp;)+", "", 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>"
;
    }
}

?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章