|
PHP-Quellcode Syntax-Highlight |
|
Dienstag, 19. September 2006 |
http://www.webmaster-resource.de/tricks/php/php-quellcode-syntax-highlight.php
Wenn Sie PHP-Quellcode mit Syntax-Hervorhebung ausgeben möchten, finden Sie hier die entsprechende Funktion.
Die folgende Funktion hebt nicht nur den Quellcode hervor, sie vergibt auch automatisch Zeilennummern zur besseren Übersichtlichkeit.
Quellcode
1.
<?php
2.
3.
function highlight($Dateiname)
4.
{
5.
ob_start();
6.
show_source($Dateiname);
7.
$I
nhalt = ob_get_contents();
8.
ob_end_clean();
9.
$Inhalt = str_replace("<code>", "", $Inhalt);
10.
$Inhalt = str_replace("</code>", "", $Inhalt);
11.
$Inhalt = str_replace("\n", "", $Inhalt);
12.
$Inhalt = explode('
', $Inhalt);
13.
$Laenge = strlen(count($Inhalt));
14.
15.
for($i = 0; $i < count($Inhalt); $i++)
16.
{
17.
$Abstand = ($Laenge-strlen($i+1));
18.
$Inhalt[$i] = '<span style="color: #999">'.str_repeat(' ', $Abstand).($i+1).' </span>'.$Inhalt[$i]."
";
19.
}
20.
21.
$Inhalt = implode('', $Inhalt);
22.
echo"<code>\n".$Inhalt."\n</code>";
23.
}
24.
25.
highlight("Quell
codeDatei.php");
26.
27.
?>
Die obige Funktion highlight(...) übernimmt einen Dateinamen (inkl. Pfad). Der Inhalt wird mit Syntax-Hervorhebung ausgegeben.
Der Text wird dann wie im obigen Quellcode formatiert. |