Hi!
Ich suche eine Möglichkeit, PHP-Code mit Syntaxhighlighting darzustellen - jedoch mit Zeilennummern - und daran scheitert meiner Erfahrung nach .phps
Es könnte Javascript sein, oder auch über PHP (wobei ich JS noch fast vorziehen würde ;-))
Bin heute zufällig auf eine interessante PHP-Funktion gestoßen:
http://www.php.net/manual/de/function.show-source.php
bzw. http://www.php.net/manual/de/function.highlight-file.php
Das ist schon wirklich gut, aber leider ohne Zeilennummern.
Eine gute Möglichkeit dafür steht in einem Kommentar, ich zitiere(so in etwa hatte ich mir das gerade auch überlegt, aber wenns schon fertig ist ;-)):
I needed to add line numbers to the output. Here is what I came up with:
Add this style to the head of your document:
<style type="text/css">
code { color: rgb(0,0,255) }
.c2 { position:absolute; left:45; white-space: nowrap; }
.nw { white-space: nowrap; }
</style>
and then use this function on a file:
function highlight_file_rownums( $file )
{
ob_start();
highlight_file($file);
$data = ob_get_contents();
ob_end_clean();
$data_lines = explode('<br />',$data);
for ($i=0, $j=count($data_lines); $i < $j; $i++) {
$k = $i + 1;
$ret .= '<div
class="nw"><code>'.$k.'</code><span
class="c2">'.$data_lines[$i]."</span></div>\n";
if (strlen($data_lines[$i]) > 450) {
$ret .= '<br />';
}
}
return $ret;
}
the ob_ is a hack because PHP returned an error when I tried to use the
second parameter to return the output as a string.
The strlen line is also a hack because the IE was wraping very long lines
on top of the next line for some reason.
Similar code to the person who contributed a note on the show_source
function. Hope it is useful to someone.
Ich hoffe das hilft Dir!
Viele Grüße
Andreas