PHP-Code highlighten (php3_highlight_string)


HTML-BODY mit hervorgehobenem PHP-Code
<body bgcolor=#eeeeee><pre> <h2>PHP-Code highlighten (php3_highlight_string) </h2><hr noshade size=1> <? include("../center/sw_string.inc.php3"); /* String-Libary */ ?> <? // ========= Funktionen ================= // // SchluesselWoerter zum highlighten $resCode = array("(",")","==","=&gt;","=",".=","!=","!","[","]","{","}","&&","||", "echo","else","for","function","if","return"); $rcChars = "()=.![]{}&|efir"; // je 1. Char der reservierten CodeWörter // Abfrage auf SchluesselWoerter function IsReservierterCode($str,$off) { global $resCode; global $rcChars; if ( strstr($rcChars, $str[$off]) ) // Optimierg: 1.Char eines Codes for($i=0; $i < count($resCode); $i++) if ( ! sw_memcmp($str, $resCode[$i], strlen($resCode[$i]),$off) ) return $resCode[$i]; return ""; } // Hervorhebung v. PHP-SchluesselWoerter in HTML-String function php3_highlight_string($htmlStr) { $co = array("php" => "<font color=#0000BB>","kom" => "<font color=#FF8000>", "hk" => "<font color=#DD0000>","res" => "<font color=#007700>"); $str = htmlspecialchars($htmlStr); $kom_on = 0; $hk_on = 0; for($i=0;$i < strlen($str); $i++) { if ( ! $kom_on ) { // Kein Kommentar if ( $str[$i] == " ") $retStr .= $str[$i]; else if ( $str[$i] == "&" && ! sw_memcmp ($str,"&quot;",6,$i)) { // HK ("") if ( $i > 0 && ($str[$i-1] == "\\") ) // demaskiert "\" $retStr .= $str[$i]; else { if ( $hk_on ) { $retStr .= "&quot;</font>"; $hk_on = 0; } else { $retStr .= $co["hk"]."&quot;"; $hk_on = 1; } $i += 5; } } else if ( ! $hk_on && $str[$i] == "/" && $str[$i+1] == "/" ) { // Kom_1 (//) $retStr .= $co["kom"]."//"; $kom_on = 1; $i++; } else if ( ! $hk_on && $str[$i] == "/" && $str[$i+1] == "*" ) { // Kom_2 (/*) $retStr .= $co["kom"]."/*"; $kom_on = 2; $i++; } else if ( ! $hk_on && ($resCode = IsReservierterCode($str,$i)) ) { // $retStr .= $co["res"].$resCode."</font>"; $i += strlen($resCode) - 1 ; } else $retStr .= $str[$i]; } else { // Kommentar $kom_on: 1 - // ; 2 - /* if ( $kom_on == 1 && $str[$i] == "\n" ) { $retStr .= "\n</font>"; $kom_on = 0; } else if ( $kom_on == 2 && $str[$i] == "*" && $str[$i+1] == "/" ) { $retStr .= "*/</font>"; $kom_on = 0; $i++; } else $retStr .= $str[$i]; } } // Hervorhebung v. "< ? .. ? >" - Bereich $retStr = str_replace("&lt;?", $co["php"]."&lt;?",$retStr); $retStr = str_replace("?&gt;","?&gt;</font>",$retStr); return $retStr; } // ========= ProgrammAblauf ================== // echo "<u><b>HTML-BODY mit hervorgehobenem PHP-Code</b></u><br>\n"; $htmlStr = implode("",file($PHP_SELF) ); $bodyStr = sw_str_htmltag($htmlStr,"body",1); echo php3_highlight_string($bodyStr); ?> </pre></body>