Jump to content


Photo

Source-Code Display


  • Please log in to reply
No replies to this topic

#1 Picatta

Picatta
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 26 March 2006 - 09:04 PM

I started learning php not too long ago, and recently decided to start a project which would let me display the sourcecode of a file and display it with line numbers and syntax highlighting. Everything was going smooth and I had gotten line numbers to work, but I can't figure out syntax highlighting.

The sourcecode so far is:
<?php
if (empty($_GET['method'])) {
print "
<html>
<head>
</head>
<body>
<form ENCTYPE=\"multipart/form-data\" action=?method=display method=POST>
Source: <br />
<input type=file name=source><br /><br />
<input type=submit name=submit value=Display>
</form>
</body>
</html>
";
} elseif ($_GET['method'] == "display") {
print "
<html>
<head>
</head>
<body>
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<tr>
<td align=center></td>
<td align=center><INPUT type=button value=BACK onClick=history.back()></td>
</tr>
<tr>
<td bgcolor=#000099 align=center><font color=#FFFFFF> # </font></td>
<td bgcolor=#000099 align=center><font color=#FFFFFF> Line </font></td>
</tr>
";
$spacer = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
$fname = file($_FILES['source']['tmp_name']);
$int = 2;
foreach($fname as $line => $fline) {
   $fline = htmlspecialchars($fline);
   print "
   <tr>
   <td bgcolor=#000099 align=center><font color=#FFFFFF> [$line] </font></td>
   <td "; if ($int % 2 != 0) {$bgcolor = "CCCCCC";} else {$bgcolor = "BBBBBB";}; print "bgcolor=$bgcolor><code> $spacer$fline</code></td>
   </tr>
   ";
   $int++;
}
print "
<tr>
<td bgcolor=#000099 height=20></td>
<td bgcolor=#000099 height=20></td>
</tr>
</table>
</body>
</html>
";
}
?>

I tried using highlight_string to highlight it, and then explode() it at the <br /> part, but since it was split into table cells it didn't work. A point in the right direction would be nice.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users