Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 16th, 2007, 9:43 PM   #1
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
Question Presenting source code within html

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
__________________

Wizard1988 is offline   Reply With Quote
Old Mar 16th, 2007, 10:01 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Mar 16th, 2007, 10:06 PM   #3
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
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.
__________________

Wizard1988 is offline   Reply With Quote
Old Mar 16th, 2007, 10:11 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Mar 17th, 2007, 2:20 AM   #5
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 437
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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>&nbsp;' . $source[$i];
	for($j = 0; $j < $spaces - strlen($i + 1); $j++){
		$source[$i] = '&nbsp;' . $source[$i];
	}
}
for($i = 0; $i < count($source); $i++){
	echo $source[$i];
}
?>
</pre>
The line in bold you would replace the string 'forum/admin.php' with the filename of your own sourcecode. Hope this helps.
__________________
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
grimpirate is offline   Reply With Quote
Old Mar 17th, 2007, 12:41 PM   #6
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
Wow, thanks a million!
__________________

Wizard1988 is offline   Reply With Quote
Old Mar 17th, 2007, 4:05 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Mar 17th, 2007, 4:47 PM   #8
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
Yes I have considered that, and I will provide a link where the users can view the plain code.

Again thanks for help!
__________________

Wizard1988 is offline   Reply With Quote
Old Mar 17th, 2007, 5:30 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 18th, 2007, 11:44 PM   #10
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 437
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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
grimpirate is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC