Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.