Page 1 of 1

Lines numbers

Posted: Fri Nov 25, 2005 12:37 pm
by HiddenS3crets
I'm trying to print out source code with a line number next to each line.

This function kind of works, but first off the code goes past the numbers.

This is my source code:

Code: Select all

$lines = explode("\n", $code);
    $numbers = "";
    $final = "";

    for($x = 1; $x <= 100; $x++) {
        $number = $x . "<br \>\n";
        $numbers .= $number;
        $line = $lines[$x - 1] . "<br \>\n";
        $final .= $line;
    }
    
    echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
  <tr>
    <td width=\"1%\" align=\"right\" valign=\"top\">
$numbers
    </td>
  
    <td width=\"1%\" valign=\"top\">
    </td>

    <td width=\"98%\" align=\"left\" valign=\"top\">
$final
    </td>
  </tr>
</table>";
}
$code being the source code to display.

A live example of this can be viewed here

Posted: Fri Nov 25, 2005 12:40 pm
by josh
because your loop only runs 100 times.


use a foreach or a while loop instead

Posted: Fri Nov 25, 2005 12:41 pm
by HiddenS3crets
I have it fixed. Thanks

Posted: Fri Nov 25, 2005 12:49 pm
by josh
because of the way you're outputting it into two columns of a table, put each line in it's own row

Code: Select all

<tr>
<td>line#</td>
<td>line of code</td>

</tr>

Posted: Fri Nov 25, 2005 2:49 pm
by John Cartwright
jshpro2 wrote:because of the way you're outputting it into two columns of a table, put each line in it's own row

Code: Select all

<tr>
<td>line#</td>
<td>line of code</td>

</tr>
That would render copy and pasting pretty much useful, I believe.
Line breaks are key.

Posted: Fri Nov 25, 2005 2:57 pm
by onion2k

Posted: Fri Nov 25, 2005 10:00 pm
by josh
Jcart wrote:That would render copy and pasting pretty much useful, I believe.
Line breaks are key.
Just fixing his code, not advocating it. Good point however

Posted: Sat Nov 26, 2005 2:18 am
by jmut
From console

Code: Select all

cat source_file_name.php  | php -R 'echo $argi.":".$argn."\n";' | php -s >  target_file_name.html