Results 1 to 6 of 6

Thread: PHP driven Syntax Highlighter

  1. #1

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    PHP driven Syntax Highlighter

    I have tried using some syntax highlights before, and they all suck

    So problem solved, i will write my own PHP function to highlight the syntax for me



    Now i would like some guidance on the general workings of this, as i cant quite figure it all out.

    Im going to need to loop through the characters of the string (the code i want to highlight), whilst picking out certain keywords, copmment lines etc to highlight.

    Anyone help? Cheersio

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP driven Syntax Highlighter

    if you're highlighting PHP syntax (it would have probably been better if you said something about the language you were using), then PHP has its own functions built in that will do it for you.

    I made a PHP source thing a while ago, so for this, you just need to basically look into the function highlight_string(). Here is an example of how you could use it:
    PHP Code:
    <?php
      $file 
    file_get_contents('index.php');
      
    highlight_string($file);
    ?>
    If you want to manipulate it (add line numbers, etc), use ob_start() before calling the function, then highlight your $file, and then ob_get_contents() to trap the HTML it outputs. Then you can just clear the buffer (ob_end_clean()) so that you can get rid of everything you saved in it. This way, you can change the contents and do your own few things without it outputting until you want it to. This is a full fledged example of that:
    PHP Code:
    <style>
      div {
        font-size: 10pt;
      }
      #source {
        border: 1px solid #000;
        display: table;
        width: 100%;
      }
      .line {
        clear: both;
        width: 100%;
        display: block;
      }
      .lineno {
        font-family: arial;
        width: 25px;
        float: left;
        padding-left: 5px;
        background-color: #efefef;
        border-right: 1px solid #c0c0c0;
      }
      .text {
        font-family: courier new;
        float: left;
        margin-left: 10px;
      }
    </style>
    <?php
      $file 
    file_get_contents('index.php');
      
    ob_start();
      
    highlight_string($file);
      
    $syntax ob_get_contents();
      
    ob_end_clean();
      
    //let's add line numbers
      
    $lines explode("\r<br />"$syntax);
    ?>
    <div id="source">
    <?php
      
    for($i 0$i count($lines); $i++){
        echo 
    '  <div class="line">' "\r\n";
        echo 
    '    <div class="lineno"><font color=#c0c0c0>' . ($i 1) . '</font></div><div class="text"><nobr>' $lines[$i] . '</nobr></div>' "\r\n";
        echo 
    '  </div>' "\r\n";
      }
    ?>
    </div>
    I just made it (only because the old script I had was somewhat badly written, and it might need a bit of tweaking on the style sheet's part for lines of text that will scroll off of the edge if you don't want them to break at all (although, the line structure will stay intact, the line will just break). I only tested it with the index.php of my local webserver, but the syntax stuff all works alright.

    if you aren't highlighting PHP syntax, then you will probably want to learn about regular expressions if you haven't already, as you'll be doing a lot of preg_replace()'s. Looping through characters is probably unneeded, and would take an awfully long time as well. Looping through lines, depending on the structure of the language you've chosen, might be needed.
    Last edited by kows; Nov 8th, 2006 at 11:06 AM.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP driven Syntax Highlighter

    You don't need to use output buffering, just tell highlight_string to return instead of outputting.

    PHP Code:
    $coloured_code highlight_string($filetrue); 

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP driven Syntax Highlighter

    oops, didn't know they added the second parameter. last time I used it was before php 4.2. thanks for the heads up, though.

  5. #5

    Thread Starter
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: PHP driven Syntax Highlighter

    OH, its for VB code :P

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP driven Syntax Highlighter

    Use geshi or preg_replace.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width