: : g_HIGHLIGHT

Balíček obsahuje funkce pro zvýraznění php kódu a trochu i kódu html. Barvy lze změnit na začátku funkcí g_phphighlight a g_htmlhighlight.
Až najdu trochu času, doplním krásnější zvýrazňování html kódu v php řetězcích.
Nějak jsem si to rozvrtal a teď dávám pomalu do kupy...


Použití:
Kdekoliv přes funkce:
ghlt($string);  (pro zvýraznění jednoho řetězce) a
g_highlight_file('soubor');  (pro zvýraznění celého souboru na disku).

Kód:
g_highlight.php
<? /* HIGHLIGHT */
$g_highlight_true = true;

  function g_phphighlight($str) {
    $color_php      = "#0000AB";
    $color_string   = "#AB0000";
    $color_integer  = "#FB0000";
    $color_other    = "#246424";
    $color_html     = "#000000";
    $color_hint     = "#848474";
    $color_function = "#0000E0";
    
    $char_tmp = 0;
    $lom = false;
    $phs = false;
    $labc = '$ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_'; 
    $lint = ',.+-*#<=>!0123456789(){}[]';
    for ($i=127;$i<255;$i++) {
      $llabc .= chr($i);
    }   
    $l=''; 
    for ($i=0; $i<strlen($str); $i++) {      
      $c = $str{$i};
      if ($c=='<') $c = '<';
      if ($c=='>') $c = '>';      

      $d = $c;
      
      if (empty($char_tmp) && !$phs && substr($str,$i,8)=='function') {
        $c = 'F_'.$c;   
        $d = 'F_'.$d;   
      }
      
      if ($c=='<' && $str{($i+1)}=='?' && empty($char_tmp)) {
        $c = '<font color="'.$color_other.'"><';
        $d = '';
      } else
      if ($c=='>' && $l=='?' && empty($char_tmp)) {
        $c = '></font>';
        $d = '';
      }
      if (empty($char_tmp) && $c=='/' && $str{($i+1)}=='/') {
        $c = '<font color="'.$color_hint.'">/';
        $char_tmp = "\n";
      }
      if (empty($char_tmp) && $c=='/' && $str{($i+1)}=='*') {
        $c = '<font color="'.$color_hint.'">/';
        $char_tmp = '*_';
      }
      if ($c=='/' && $l=='*' && $char_tmp == '*_') {
        $c .= '</font>';
        $char_tmp = 0;
        $lom=false;
      }
      
      if ($d=='\\' && ($char_tmp=='"' || $char_tmp=="'")) {
        $lom = !$lom;
      }
        
      if (empty($char_tmp)) {
        switch($d) {
          case "'": $char_tmp = "'"; break;
          case '"': $char_tmp = '"'; break;
        }
        if (!empty($char_tmp)) {
          $c .= '<font color="'.$color_string.'">';
          $lom = false;
        }
      } else
        if ($d==$char_tmp) {
          if (!$lom) {
            $char_tmp = 0;
            $c = '</font>'.$c;
          }
        }
        
      if ($phs && strpos($labc,$d)===false) {
        $phs = false;
        $c = '</font>'.$c;
      }
           
      if ((empty($char_tmp) || $char_tmp=='"') && $d=='$' && !$phs) {
        $c = '<font color="'.$color_php.'">'.$c;
        $phs = true;
      }

      if (!$phs && empty($char_tmp)) {
        $n = '';
        if (!empty($d) && (strpos($lint,$d) !== false || is_numeric($d))) {
          $n = '<font color="'.$color_integer.'">'.$d;
          $i++;
          if ($i<strlen($str)) {
            while ($i<strlen($str)) {
              $m = $str{$i};
              if (strpos($lint,$m) !== false) {
                if ($m=='<') $m = '<';
                if ($m=='>') $m = '>';      
                $n .= $m;
                $i++;
              } else {
                $i--;
                break;
              }
            }
          } 
          $n.='</font>';
          if ($c!=$d) $c = substr($c,0,-1).$n;
          else $c = $n;
        }
      }

      if ($d!='\\') $lom=false;

      $ret_str .= $c;
      $l = $d;
    }
    $ret_str = preg_replace("@F_function( )(\w+)<@si", '<a name="$2"></a><a href="#"><b>function</b></a> <font color='.$color_function.'>$2</font><', $ret_str);
    return $ret_str; 
  }
  
  function g_htmlhighlight($str) {
    $color_base = "#000000";
    $color_tag  = "#005098";
    $color_hint = "#646454";
    $ret_str = 
str_replace('<','<font color='.$color_tag.'><',
str_replace('>','></font>',
str_replace('<','<',
str_replace('>','>',
$str
))));
    return $ret_str;
  }
  
  function g_highlight_string($str) {    
    $ret_str = "\n".'<pre style="background: #EEEEEE; margin-right: 10px; width: 100%; display: block">';    
    $ret_str .= g_phphighlight($str);
    $ret_str .= '</pre>';
    return $ret_str;
  }
  
  function ghlt($str) {
    return g_highlight_string($str); 
  }
  
  function ghlts($str) {
    return '<table border=0 bgcolor=#EEEEEE><tr><td>'.ghlt($str).'</td></tr></table>';    
  }

  function g_highlight_file($file) {
    if (file_exists($file) && is_readable($file)) {
      $f = fopen($file,"r");
      if ($f) {
        $str = '';
        while (!feof($f)) 
          $str .= fgets($f,512);
        fclose($f);
        //return '<i>'.$file.'</i>'.str_replace('</font','</font',str_replace('<font','<font',ghlt($str)));

        return '<i>'.$file.'</i>'.ghlt($str);
      }
      fclose($f);
    }
    return false;
  }

?>