html_highlighter.php


Quell Code


<?php
function highlight_html($tmpCode){
  if(!defined('HLH_TAG')){
    # Highlight-Farben
    define('HLH_TAG', '#d02'); // HTML-Tag
    define('HLH_ATTR', '#00d'); // HTML-Tag-Attribut
    define('HLH_ATTR_VAL', '#090'); // HTML-Tag-Attribut-Wert
    define('HLH_JS', '#399'); // JavaScript
    define('HLH_PHP', '#970'); // PHP
    define('HLH_COMM', '#777'); // HTML-Kommentar
    define('HLH_ENT', '#e60'); // Entity
  }
  $tmpCode = htmlspecialchars($tmpCode);
  
  $tmpCode = str_replace("\t", '    ', $tmpCode);
  $tmpCode = str_replace(' ', '&nbsp;', $tmpCode);
  $tmpCode = str_replace('=', '&#61;', $tmpCode);
  # PHP-Code
  $tmpCode = preg_replace_callback('~&lt;\?(.*?)\?&gt;~is', 'hlh_htmlphp', $tmpCode);
  # Javascript
  $tmpCode = preg_replace_callback('~&lt;script(.*?)&gt;(.*?)&lt;/script&gt;~is', 'hlh_htmljs', $tmpCode);
  # Kommentar
  $tmpCode = preg_replace_callback('~&lt;!--(.*?)--&gt;~is', 'hlh_htmlcomm', $tmpCode);
  # Start-Tag (ohne Attribute)
  $tmpCode = preg_replace('~&lt;([a-z!]{1}[a-z0-9]{0,})&gt;~is', '&lt;<span style="color:'.HLH_TAG.'">$1</span>&gt;', $tmpCode);
  # Start-Tag
  $tmpCode = preg_replace_callback('~&lt;([a-z!]{1}[a-z0-9]{0,})&nbsp;(.*?)&gt;~is', 'hlh_htmltag', $tmpCode);
  # End-Tag
  $tmpCode = preg_replace('~&lt;\/([a-z]{1}[a-z0-9]{0,})&gt;~is', '&lt;/<span style="color:'.HLH_TAG.'">$1</span>&gt;', $tmpCode);
  # Entity
  $tmpCode = preg_replace('~&amp;([#a-z0-9]{1,20});~i', '<span style="color:'.HLH_ENT.'">&amp;$1;</span>', $tmpCode);
  
  $tmpCode = nl2br($tmpCode);
  return '<code>'.$tmpCode.'</code>';
}

function hlh_htmltag($code){ # Callback für Start-Tags
  $tag_name = $code[1];
  $tag_fill = $code[2];
  $tmp_tag = '&lt;<span style="color:'.HLH_TAG.'">'.$tag_name.'</span>&nbsp;';
  $tmp_tag.= preg_replace_callback('~([a-z-]+)&#61;([a-z0-9]{1,}|&quot;(.*?)&quot;|\'(.*?)\')~i', 'hlh_htmltagattr', $tag_fill);
  $tmp_tag.= '&gt;';
  return $tmp_tag;
}
function hlh_htmltagattr($code){ # Callback für Attribute
  $attr_name = $code[1];
  $attr_valwq = $code[2];
  return '<span style="color:'.HLH_ATTR.'">'.$attr_name.'</span>&#61;<span style="color:'.HLH_ATTR_VAL.'">'.$attr_valwq.'</span>';
}
function hlh_htmljs($code){ # Callback für Javascript
  $tag_fill = $code[1];
  $jsCode = $code[2];
  $jsCode = str_replace('&lt;', '&#60;', $jsCode);
  $jsCode = str_replace('&gt;', '&#62;', $jsCode);
  $jsCode = str_replace('&amp;', '&#38;', $jsCode);
  return '&lt;script'.$tag_fill.'&gt;<span style="color:'.HLH_JS.'">'.$jsCode.'</span>&lt;/script&gt;';
}
function hlh_htmlphp($code){ # Callback für PHP
  $phpCode = $code[1];
  $phpCode = str_replace('&lt;', '&#60;', $phpCode);
  $phpCode = str_replace('&gt;', '&#62;', $phpCode);
  $phpCode = str_replace('&amp;', '&#38;', $phpCode);
  $phpCode = str_replace('&quot;', '&#34;', $phpCode);
  $phpCode = str_replace("'", '&#39;', $phpCode);
  return '<span style="color:'.HLH_PHP.'">&#60;?'.$phpCode.'?&#62;</span>';
}
function hlh_htmlcomm($code){ # Callback für Kommentare
  $phpCode = $code[1];
  $phpCode = str_replace('&lt;', '&#60;', $phpCode);
  $phpCode = str_replace('&gt;', '&#62;', $phpCode);
  $phpCode = str_replace('&amp;', '&#38;', $phpCode);
  return '<span style="color:'.HLH_COMM.'">&#60;!--'.$phpCode.'--&#62;</span>';
}
?>
 
<?php
$code = '<div style="color:olive;">
 <strong>nur zwei &euro;!</strong>
</div>';//Dieser Code wird  dann in Farbe dargestellt
echo highlight_html($code);
?>