View Single Post
Old Mar 17th, 2007, 4:49 AM   #2
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
Nice script, very useful =D

except when I run it as-is, the first line, <?php, is on the second line uncounted and line 1 is left blank

so I went ahead and made an attempt somewhat based on your script:
[php]<?php
$file = 'script.php';

ob_start();
highlight_file($file);
$source = ob_get_contents();
ob_end_clean();

echo '<pre>';
$blocks = explode("\n", $source);
$lines = str_replace('&nbsp;', ' ', explode(chr(13), $blocks[1]));
for ($i = 0; $i < count($lines); $i++)
{
for ($j = 0; $j < strlen(count($lines)) - strlen($i + 1); $j++)
{
echo '&nbsp;';
}
$lines[$i] = (($i == 0) ? '<br />' : '') . $lines[$i];
echo '<span style="color: #777777">' . ($i + 1) . '</span> ' . substr($lines[$i], 6) . "\n";
}
echo '</pre>';
?>[/php]
Notes:
replacing &nbsp; with a space reduces the file size also, there seems to be a difference between "\n" and chr(13) from highlight_file, so splitting it into blocks first with "\n" will let you get what you want (the content itself) to split it with chr(13), which removes that first extra new line.
Styx is offline   Reply With Quote