Steffen Gebert: PHP-Code mit Syntax-Highlighting und Zeilennummern

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 ;-))

  1. 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

    1. Hallo du da draußen,

      ich habe mir auch so ein Quellcode-Anzeig-Viech gebastelt, das sieht so aus:

      <?php
       if($QUERY_STRING!="")
       {
        if(is_file($QUERY_STRING))
        {
         $titel = "Quelltext von ";
         $titel .= str_replace("./","",$QUERY_STRING);
        }
        elseif(is_dir($QUERY_STRING))
        {
         $titel = "Inhalt von ".str_replace("//","/",str_replace("//","/",str_replace(".","/",$QUERY_STRING)));
        }
        else{
         $titel = "Fehler";
        }
       }
       else
       {
        $titel = "Inhalt von /";
       }
       $countername = "quelltext";
       require("../all/head.php");
      ?>
       <body>
      <?php
       if(is_file($QUERY_STRING))
       {
        $usr_cnt=0;

      $fname = $QUERY_STRING;

      $f = file($fname);

      foreach($f as $aux_line)
        {
         $zeile = $usr_cnt + 1;
         $usr[$usr_cnt] = $zeile.": ".$aux_line;
         $usr_cnt++;
        }
        for ($idx = 0; $idx < $usr_cnt; $idx++)
        {
         $quelltext .= str_replace($rightusername,"Dies wurde aus Sicherheitsgründen aus dem Quellcode herausgeschnitten",
          str_replace($rightpasswd,"Dies wurde aus Sicherheitsgründen aus dem Quellcode herausgeschnitten",
          str_replace("Dogfish", "Dies wurde aus Sicherheitsgründen aus dem Quellcode herausgeschnitten",
          str_replace("173043", "Dies wurde aus Sicherheitsgründen aus dem Quellcode herausgeschnitten", $usr[$idx]))));
        }
        echo "<code><pre><nobr>";
        echo highlight_string($quelltext);
        echo "</nobr></pre></code>";
       }
       else
       {
        if(!is_dir($QUERY_STRING))
        {
         $QUERY_STRING = ".";
        }
        if($QUERY_STRING=="")
        {
         $QUERY_STRING=".";
        }
        $verz = opendir($QUERY_STRING);
        chdir($QUERY_STRING);
        ?>
        <h1>
         Hier gibt es die Quelltexte der einzelnen Seiten meiner Homepage
        </h1>
        <h3>
        <dir>
      <?php
        $datei_cnt = 0;
        $dir_cnt = 0;
        while($file = readdir($verz))
        {
         if(is_file($file)){$dateiname[$datei_cnt] = $file;$datei_cnt++;}
         if(is_dir($file)){$dirname[$dir_cnt] = $file;$dir_cnt++;}
        }
        sort($dirname);
        sort($dateiname);
        for($i=0;$i<count($dirname);$i++)
        {
         if($QUERY_STRING==".")
         {
          if($dirname[$i] != "." && $dirname[$i] != "..")
          {
           echo "<a href = "index.php?$QUERY_STRING/$dirname[$i]"><img src = "Grafiken/dir.png"> $dirname[$i]</a><br>";
                                }
         }
         else
         {
          if($dirname[$i] != ".")
          {
           echo "<a href = "index.php?$QUERY_STRING/$dirname[$i]"><img src = "Grafiken/dir.png"> $dirname[$i]</a><br>";
          }
         }
        }
        for($i=0;$i<count($dateiname);$i++)
        {
         echo "<a href = "index.php?$QUERY_STRING/$dateiname[$i]"><img src = "Grafiken/file.png"> $dateiname[$i]</a><br>";
        }
        closedir($verz);
      ?>
        </dir>
        </h3>
      <?php
       }
      ?>
       </body>
      </html>
      <!--

      Dabei ist jetzt nicht alles zu verstehen, aber das Grundsystem schon. Es zeigt übrigens die Zeilennummern an.

      Grüße von hier drinnen, aus Biberach,

      Dogfish