Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Web Development Languages (http://www.programmingforums.org/forum40.html)
-   -   Presenting source code within html (http://www.programmingforums.org/showthread.php?t=12834)

Wizard1988 Mar 16th, 2007 8:43 PM

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

titaniumdecoy Mar 16th, 2007 9:01 PM

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.

Wizard1988 Mar 16th, 2007 9:06 PM

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.

titaniumdecoy Mar 16th, 2007 9:11 PM

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.

grimpirate Mar 17th, 2007 1:20 AM

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.

Wizard1988 Mar 17th, 2007 11:41 AM

Wow, thanks a million!

titaniumdecoy Mar 17th, 2007 3:05 PM

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.

Wizard1988 Mar 17th, 2007 3:47 PM

Yes I have considered that, and I will provide a link where the users can view the plain code.

Again thanks for help!:)

DaWei Mar 17th, 2007 4:30 PM

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.

grimpirate Mar 18th, 2007 10:44 PM

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?


All times are GMT -5. The time now is 5:17 AM.

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