SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Colored PHP Syntax

    I want to write a class that color codes PHP syntax like on this forum (I know they already exist but I want to code my own for the experience). Are there any suggestions on what approach to take while doing this. One approach I have thought of is to use a bunch of preg_replaces but I know regex can slow things down if I do a lot of it. Another approach is to split the code into an array of PHP and non PHP (outside PHP tags), and then run regex on the PHP code while leaving the non PHP code alone, and then putting the array back together as one. I have a feeling this will take me a while to do and don't want to take one approach, then realize a different approach would have been better. Thanks,
    Matt
    Kayzio - We don't hesitate, we accelerate.

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have never experienced any slowdown due to the use of regular expressions.
    My first approach would indeed be preg_replace.

    You could then construct a couple of arrays like this:
    PHP Code:
    $functions = array ("echo","print","strlen","strstr","array","strstr");
    $keywords = array ("function","class","new","if","else","case"); 
    ...and surround all occurrences of these words with some HTML tags.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea I thought about doing something like that Ize, but then I thought that I might as well right a regex expression to match functions rather than a large array b/c a user can create their own functions which I will need to color code.
    Kayzio - We don't hesitate, we accelerate.

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what I have so far:
    PHP Code:
    <?php

        
    class phpHighlight {

            
    /**
            * Comments Color
            *
            @ public string
            */
            
    public $CommentsColor "#ff9900";

            
    /**
            * Default Color
            *
            @ public string
            */
            
    public $DefaultColor "#0000cc";

            
    /**
            * Keywords Color
            *
            @ public string
            */
            
    public $KeywordsColor "#006600";

            
    /**
            * Other Color
            *
            @ public string
            */
            
    public $OtherColor "#000000";

            
    /**
            * String Color
            *
            @ public string
            */
            
    public $StringColor "#cc0000";

            
    /**
            * Highligting Function
            *
            * @param string
            */
            
    public function highlightCode($Contents$Escaped false) {
                if (!
    $Escaped) {
                    
    $Contents htmlspecialchars($Contents);
                }
                
    $Contents preg_replace("/(&lt;\?php|&lt;\?|\?&gt;)/si""<span style=\"color:" $this->DefaultColor ";\">$1</span>"$Contents);
                
    $Contents preg_replace("/(\\$[a-z0-9_-]+?)(-&gt;)([a-z0-9_-]+?)( |;)/si""<span style=\"color:" $this->DefaultColor ."\">$1</span><span style=\"color:" $this->KeywordsColor ."\">$2</span><span style=\"color:" $this->DefaultColor ."\">$3$4</span>"$Contents);
                
    $Contents preg_replace("/(((\\$)([a-z0-9_-]+?))( |\)|;))/si""<span style=\"color:" $this->DefaultColor ";\">$2</span>$5"$Contents);
                return 
    $Contents;
            }

        }

    ?>
    This makes the PHP begin and end tags blue along with varaibles. The problem I have found with this is if you do the following:
    PHP Code:
    <?php
        
    echo "<?php ?>";
    ?>
    the PHP begin and end tags in the string are also blue. What would be an easy solution to this. Is there a way in regex to say that the string im trying to replace can't be between PHP begin and end tags. Hopefully my regex doesn't confuse you. Thanks,
    Matt
    Kayzio - We don't hesitate, we accelerate.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did this:
    PHP Code:
    $Contents preg_replace("/(&lt;\?php|&lt;\?)+(.*)(\?&gt;)+/si""<span style=\"color:" $this->DefaultColor ";\">$1</span>$2<span style=\"color:" $this->DefaultColor ";\">$3</span>"$Contents); 
    But that only matches 1 time so it grabs the first begin and last end tags.
    Kayzio - We don't hesitate, we accelerate.

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't simply use a regex -- You have to parse the code. PHP has a very powerful mechanism for tokenising PHP source-code. Have a look at token_get_all

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea I had a feeling it wasn't possible with regex only but thought I would see what I could do, thanks for the tip.
    Kayzio - We don't hesitate, we accelerate.


Bookmarks

Posting Permissions

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