![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
For a school project a teacher asked me to make a php tutorial website. I want to be able to display php code within the html of the page. I also want to automatically add line numbers to the displayed code and? Is there an easy solution to this?
Thanks in advance |
|
|
|
|
|
#2 |
|
Expert Programmer
|
You could have each line as an item in an ordered list and use CSS to display line numbers. There may be a better way to do this, but that's all I can think of.
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
I wouldn't mind doing it using an ordered list, but I really want to avoid doing it by hand if you know what I mean.
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
You could have a PHP script generate the output, although I agree that this solution is less than ideal. Hopefully someone will suggest a better solution.
|
|
|
|
|
|
#5 |
|
King of Portal
|
here's a small script I wrote up to help you achieve what you want to achieve, AND it's in PHP ^_^
<pre>
<?php
ob_start();
highlight_file('forum/admin.php');
$source = ob_get_contents();
ob_end_clean();
$source = explode(chr(13), $source);
$temp = substr($source[0], 0, 35);
$temp .= substr($source[0], 36);
$source[0] = substr($temp, 6);
$temp = substr($source[count($source) - 1], 0, strlen($source[count($source) - 1]) - 8);
$source[count($source) - 1] = $temp;
for($i = 1; $i < count($source); $i++){
$source[$i] = substr($source[$i], 6);
}
for($i = 0; $i < count($source) - 1; $i++){
$source[$i] .= '<br />';
}
$spaces = strlen(count($source));
for($i = 0; $i < count($source); $i++){
$source[$i] = '<span style="color: #777777">' . ($i + 1) . '</span> ' . $source[$i];
for($j = 0; $j < $spaces - strlen($i + 1); $j++){
$source[$i] = ' ' . $source[$i];
}
}
for($i = 0; $i < count($source); $i++){
echo $source[$i];
}
?>
</pre>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
Wow, thanks a million!
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
You want to consider what will happen when the user copies and pastes the text on your site. Using both my method and grimpirate's, the line numbers will be included, which will be a great nuisance to users. I suggest providing a "download source" link for each code snippet on your site, or finding a better way to add line numbers.
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
Yes I have considered that, and I will provide a link where the users can view the plain code.
Again thanks for help! ![]() |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I haven't thought it through, myself, but since <pre> text doesn't add line-wrapping not in the original, have you considered making the line numbers part of the container? Even if a logical line wraps, you could accomodate it with a blank in the line number column.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
King of Portal
|
I'm not entirely sure I understand what you're saying DaWei, I posted the script here at the PHP forum if you could explain a bit further. I just kinda threw this together to help out Wiz, but I'm willing to try and make it better if I can understand what you're getting at. Possibly like structuring it in a table?
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| start Learning HTMl | smita | HTML / XHTML / CSS | 0 | Mar 15th, 2007 8:38 AM |
| GCC sprintf Source Code | JawaKing00 | C | 2 | Nov 30th, 2006 4:53 AM |
| Basic HTML Tutorial - Reuben Keeney | ReubenK | HTML / XHTML / CSS | 14 | Mar 26th, 2006 6:50 AM |
| Image processing:Stentiford and Holt Thinning Source code in C or C++ | ladyscarlet99 | C | 0 | Sep 9th, 2005 2:20 AM |