Tjo, ich hab jetz ma nen syntax-highlighter für html geschrieben (unterstützt weder includiertes css, php noch js)
hier der php-code:
Code
function syntax_high($code, $in_com) {
$code = str_replace('"', '"', $code);
if (count(explode("<!--", $code)) > 1) {
$code = ereg_replace('<!--(.*)', '<span class="comment"><!--\1', $code);
$in_com = true;
} elseif (count(explode("-->", $code)) > 1) {
$code = ereg_replace('(.*)-->', '\1--></span>', $code);
$in_com = false;
} elseif ($in_com) {
$code = ereg_replace('(.*)', '<span class="comment">\1</span>', $code);
} else {
$code = ereg_replace('([^ ]*)="([^"]*)"', '<span class="attr">\1</span>="<span class="attr_val">\2</span>"', $code);
$code = ereg_replace('<([^&]*)>', '<span class="lt"><</span><span class="tag">\1</span><span class="gt">></span>', $code);
$code = ereg_replace('</([^&]*)>', '<span class="lt"></</span><span class="tag">\1</span><span class="gt">></span>', $code);
}
return array($in_com, $code);
}
function echo_code($text) {
$return = '';
$return .= '<pre>'."\n";
$parts = explode("\n", $text);
$max = (string)count($parts);
$length = strlen($max);
$sy_text = '';
$in_com = false;
$sy_text_array = array();
foreach ($parts as $key=>$val) {
$sy_text_array = syntax_high(htmlentities($val), $in_com);
$in_com = $sy_text_array[0];
$sy_text .= ' '.$sy_text_array[1]."\n";
}
$parts = explode("\n", $sy_text);
$max = (string)count($parts);
$length = strlen($max);
foreach ($parts as $key=>$val) {
$num = $length - strlen($key + 1);
$return .= '<span class="num">';
for($i=0;$i<$num;$i++) {$return .= '0';}
$return .= ($key + 1).'</span>';
$return .= $val."\n";
}
$return .= '</pre>';
return $return;
}
Alles anzeigen
das css zum gestalten der farbigen ausgabe:
Code
pre span.lt, pre span.gt {
color:#0000ff;
}
pre span.tag {
color:#0000ff;
}
pre span.attr {
color:#ff0000;
}
pre span.attr_val {
color:#ff00ff;
}
pre span.comment {
color:#00cc00;
}
pre span.num {
color:#555555;
}
Alles anzeigen
hier ein besipiel-funktionsaufruf:
und die ausgabe:
oder so ähnlich