Jump to content

Source-Code Display


Picatta

Recommended Posts

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:
[code]
<?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>
";
}
?>
[/code]

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.
Link to comment
https://forums.phpfreaks.com/topic/5876-source-code-display/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.