Hey guys. Just thought I'd let you into my little world for a bit.
After getting tired of having to constantly write out PHP code, I decided to write a class that can be used by anyone, and have it give line-numbers and highlight a file. I'm pretty sure I've got it working properly. It seems okay to me, but I'm just lookin for pointers, praise, and comments.
I don't mind criticism, as long as it is constructive.
Features
Smart Line-Numbering
Custom Line Number color
Easy implementation
Here's the class code:
<?php
class Highlight
{
var $i;
var $k;
var $contents;
var $data;
var $lines;
var $text;
var $lnum;
function get_highlight($filename)
{
$this->contents = highlight_file($filename, TRUE);
return $this->contents;
}
function number_it($contents, $color)
{
function check($num, $lines, $color)
{
for($k=0; $k < (strlen($lines)-strlen($num)); $k++)
{
$this->lnum .= ' ';
}
$num = '<font color="'.$color.'">'.$this->lnum.$num.'</font>';
return $num;
}
$this->data = explode('<br />', $contents);
$this->lines = count($this->data);
$this->i = 1;
foreach($this->data as $line)
{
$this->text .= check($this->i, $this->lines, $color).' '.str_replace("\n", '', $line).'<br />'."\n";
$this->i++;
}
return $this->text;
}
}
?>
Now, it's called in only two lines:
<?php
/*
+--------------------------------+
| TEST THE HIGHLIGHT CLASS |
+--------------------------------+
*/
// Some pointless php code
include_once('highlight.class.php');
$hghlt =& new Highlight;
$file = $hghlt->get_highlight(__FILE__);
$file2 = $hghlt->number_it($file, '#000000');
print '<span style="margin: 5px 0 10px 0; float: left; width: 800px; height: auto; border: 2px groove #f8f8f8; background-color: #fefefe; text-align: left; padding: 5px;">
<code>'."\n";
echo $file2;
print '</code>
</span>';
?>
Check it out in action here:
Test Highlight Class
Let me know what you think, and any upgrades i could make.
Future Plans
One Line declaration
Custom output style
¿Highlight HTML as well?
~Brett