numbered code lines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jgentes
    New Member
    • Jul 2007
    • 32

    numbered code lines

    I really like the numbered code lines on this forum. I would like to incorporate something like this on my own site, and was wondering how exactly it was done? I am assuming that its creating some type of loop, delimiting by newline character, and inserting the n+1 where n is the number last new line character integer.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You could set up a loop, searching for \n characters using the strpos() function, which would give you their position. Meanwhile, you could use the substr_replace( ) function, inside the loop, to replace each of the new-line characters with an integer that is incremented each time.

    If that's complete gibberish, please let me know and I'll try to rephrase.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, JGentes.

      Actually, TheScripts uses an ordered list.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by pbmods
        Heya, JGentes.

        Actually, TheScripts uses an ordered list.
        That's way to simple!... I'd never have though of that :D

        Comment

        • jgentes
          New Member
          • Jul 2007
          • 32

          #5
          so you explode them to an array by some newline delimiter? then implode them using an ordered list?

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            I would assume so yes.

            Somewhat like this:
            [code=php]
            <ol>
            <?php
            $arr = explode("\n", $yourCode);
            foreach($arr as $line) {
            echo "<li>$line</li>";
            }
            ?>
            </ol>
            [/code]

            Comment

            Working...