HTML-Highlighter

September 2009

In PHP gibt es eine Funktion, die sich highlight_string() nennt. Wenn es die nun auch für HTML und XML-Code gäbe, wäre es perfekt.
Also kreieren wir sie: highlight_html()

<?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>';
}
?>
Zu Beginn werden mit den Konstanten HLH_TAG, HLH_ATTR etc. die Farben definiert, mit welchen der Code hervorgehoben werden soll.
Anschliessend wird der String so bearbeitet, dass er sich leichter handhaben lässt.
Und dann kommt das eigentliche Highlighting: Zuerst werden eventuell vorhandener PHP- und JavaScript-Code sowie HTML-Kommentare verarbeitet, damit diese im weiteren Vorgehen nicht stören.
Schliesslich wird dann der HTML-Code mit verschiedenen Callback-Funktionen bearbeitet.

Aufgerufen wird die Funktion wie folgt:
<?php
$code 
'<div style="color:olive;">
 <strong>nur zwei &euro;!</strong>
</div>'
;
echo 
highlight_html($code);
?>
Mögliches Ergebnis:
<div style="color:olive;">
 <strong>nur zwei &euro;!</strong>
</div>

Mit dieser Funktion lassen sich alle XML-ähnlichen Sprachen einfärben, wie beispielsweise SVG oder MathML.

Siehe auch:
 


Andere Einträge