PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
HiddenS3crets
Forum Contributor
Posts: 119 Joined: Fri Apr 22, 2005 12:23 pm
Location: USA
Post
by HiddenS3crets » Fri Nov 25, 2005 12:37 pm
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
josh
DevNet Master
Posts: 4872 Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida
Post
by josh » Fri Nov 25, 2005 12:40 pm
because your loop only runs 100 times.
use a foreach or a while loop instead
josh
DevNet Master
Posts: 4872 Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida
Post
by josh » Fri Nov 25, 2005 12:49 pm
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>
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Fri Nov 25, 2005 2:49 pm
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.
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Fri Nov 25, 2005 2:57 pm
josh
DevNet Master
Posts: 4872 Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida
Post
by josh » Fri Nov 25, 2005 10:00 pm
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
jmut
Forum Regular
Posts: 945 Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:
Post
by jmut » Sat Nov 26, 2005 2:18 am
From console
Code: Select all
cat source_file_name.php | php -R 'echo $argi.":".$argn."\n";' | php -s > target_file_name.html