Geschrieben von gollum1990 am 13.07.2006, 17:16

In dieses Tutorial wird gezeigt, wie man HTMl Codes und CSS Codes "gehighlightet" werden.
Script:

<?php // HTML highlighter //

function highlight_html_string($code) {

$code = str_replace(" "," ",$code);
$code = str_replace(" "," ",$code);

$code = str_replace("'","{s-}",$code);
$code = str_replace('"','{s=}',$code);
$code = str_replace('<','{t-}',$code);
$code = str_replace('>','{t=}',$code);

$code = preg_replace('/{t-}!--(.*?)--{t=}/is','<span style="color: orange;"><!--\1--></span>',$code);
$code = preg_replace('/{t-}(.*?){t=}/i','<span style="color:blue;"><\1></span>',$code);
$code = str_replace('{t-}','<',$code);
$code = str_replace('{t=}','>',$code);
$code = preg_replace('/{s(-|=)}(.*?){s\1}/is','<span style="color:red;">"\2"</span>',$code);

return '<code>'.$code.'</code>';

}

// CSS highlighter //

function highlight_css_string($code) {

$code = str_replace(':','%t-%',$code);
$code = str_replace(';','%t=%',$code);

$code = str_replace(" "," ",$code);
$code = str_replace(" "," ",$code);

$code = preg_replace('/\[(.*)\/\/(.*)\]/isU','[\1°°\2]',$code);

$code = preg_replace('/{(.*?)}/is','{<span style="color: blue;">\1</span>}',$code);
$code = preg_replace('/%t-%(.*?)%t=%/i',':<span style="color: red;">\1</span>;',$code);

$code = str_replace('%t-%',':',$code);
$code = str_replace('%t=%',';',$code);

$code = preg_replace('/\/\*(.*?)\*\//is','<span style="color: green;">/*\1*/</span>',$code);
$code = preg_replace('/\/\/(.*)\s/','<span class="css_com">//\1</span>',$code);
$code = preg_replace('/#([0-9A-Fa-f]{3,6})/i','<a href="#" class="css_color" onmouseover="this.style.backgroundColor = this.style.color = \'#\1\';" onmouseout="this.style.backgroundColor = this.style.color = \'\';">#\1</a>',$code);

$code = preg_replace('/\[(.*)°°(.*)\]/isU','[\1//\2]',$code);

return '<code><span class="css_black">'.$code.'</span></code>';

}
?>

Das Script wird dann so aufgerufen:


<?php
include 'html_und_css_highlighter.php';
$htmlcode = <<<HTML
<html>
<head>
<title>Hallo</title>
</head>
<body>
<!-- Kommentar -->
<a href="bla">Blub</a>
</body>
</html>
HTML;

$csscode = '<style type="text/css">';
$csscode .= ' td { color: blue; } ';
$csscode .= '</style>';
echo highlight_html_string($html);
echo highlight_css_string($csscode);
?>

Fertig.

Bewertung Anzahl
6
18,2 %
2 Bewertungen
5
27,3 %
3 Bewertungen
4
9,1 %
1 Bewertungen
3
27,3 %
3 Bewertungen
1
18,2 %
2 Bewertungen