HTML Syntax Highlighter
Posted: Mon Oct 13, 2003 10:32 am
Okay, so I was bored. I just though of a way of a simple highlighting of a HTML script from Dreamweaver's colors. Here it is:
Now, since I wrote it, it's obvious that It doesn't work so correctly. No errors are thrown up so I'm scratching my head a little on this one O_o.
-Nay
Code: Select all
<html>
<head>
<title>HTML Syntax Highlighter</title>
<style type="text/css">
body {
font-family:verdana;
font-size:xx-small;
}
.code {
font-family:courier new;
font-size:x-small;
background:#dcdcdc;
width:500px;
border-width:1;
border-style: solid;
border-color: #999999;
padding:5px;
}
textarea {
font-family:courier new;
font-size:x-small;
background:#ffffff;
width:500px;
height:100px;
border-width:1;
border-style: solid;
border-color: #000000;
scrollbar-face-color: #ffffff;
scrollbar-shadow-color: #ffffff;
scrollbar-darkshadow-color: #ffffff;
scrollbar-highlight-color: #ffffff;
scrollbar-3dlight-color: #ffffff;
scrollbar-track-color: #ffffff;
scrollbar-arrow-color: #cccccc;
}
input {
font-family:verdana;
font-size:xx-small;
background:#cccccc;
border-width:1;
border-style: solid;
border-color: #000000;
}
</style>
</head>
<body>
<p>
Highlighted Code:
</p>
<div class="code">
<?php
$action = $_GET['action'];
if(isSet($action)) {
if(!empty($_POST['code'])) {
$code = $_POST['code'];
htmlspecialchars($code);
// tags
$syntax = array();
$syntax[0] = "b";
$syntax[1] = "strong";
$syntax[2] = "i";
$syntax[3] = "em";
$syntax[4] = "u";
$syntax[5] = "a";
$syntax[6] = "font";
$syntax[7] = "script";
$syntax[8] = "style";
$syntax[9] = "note";
// tag's colors
$colors = array();
$colors['b'] = "0066ff";
$colors['strong'] = "0066ff";
$colors['i'] = "0066ff";
$colors['em'] = "0066ff";
$colors['u'] = "0066ff";
$colors['a'] = "00cc33";
$colors['font'] = "0000ff";
$colors['script'] = "cc3300";
$colors['style'] = "cc00ff";
$colors['note'] = "6666cc";
$all = count($syntax);
for($i=0;$i<$all;$i++) {
$tag = $syntax[$i];
$color = $colors[$tag];
$start = "<" . $tag;
$end = "</" . $tag . "><br />";
$s_replace = "<span style="color:" . $color . ";">" . start;
$e_replace = $end . "</span>";
$code = str_replace($start, $s_replace, $code);
$code = str_replace($end, $e_replace, $code);
echo $code;
}
} else {
echo "Please insert the HTML code below"; // if the posted code is blank
}
} else {
echo "Please insert the HTML code below"; // if the code is NOT posted
}
?>
</div>
<br />
<form name="script" method="post" action="?action=highlight">
<textarea name="code"></textarea><br />
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
</body>
</html>-Nay