Results 1 to 7 of 7

Thread: C# Syntax Highlighter

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188

    C# Syntax Highlighter

    I am having some problems with multiline comments stripping the color from other text.

    PHP Code:
    function cshilite($code)
    {

        
    $code str_replace("<br>"""$code);
        
    $code str_replace("<br />"""$code);
        
    $code str_replace("&gt;"">"$code);
        
    $code str_replace("&lt;""<"$code);
        
    $code str_replace("&quot;""\\"", $code);
        
    $code = str_replace("&amp;", "&", $code);
        
    $code = str_replace('$', '\\$', $code);
        
    $code = str_replace('\\n', '\\\\\\\\n', $code);
        
    $code = str_replace('\\r', '\\\\\\\\r', $code);
        
    $code = str_replace('\\t', '\\\\\\\\t', $code);

        
    $code = stripslashes($code);

        
    $code = htmlentities($code);

        
    $keywords = array('abstract', 'event', 'new', 'struct', 'as', 'explicit', 'null', 'switch', 'base', 'extern', 'object', 'this', 'bool', 'false', 'operator', 'throw', 'break', 'finally', 'out', 'true', 'byte', 'fixed', 'override', 'try', 'case', 'float', 'params', 'typeof', 'catch', 'for', 'private', 'uint', 'char', 'foreach', 'protected', 'ulong', 'checked', 'goto', 'public', 'unchecked', 'class', 'if', 'readonly', 'unsafe', 'const', 'implicit', 'ref', 'ushort', 'continue', 'in', 'return', 'using', 'decimal', 'int', 'sbyte', 'virtual', 'default', 'interface', 'sealed', 'volatile', 'delegate', 'internal', 'short', 'void', 'do', 'is', 'sizeof', 'while', 'double', 'lock', 'stackalloc', 'else', 'long', 'static', 'enum', 'namespace', 'string');

        for (
    $x = 0; $x < count($keywords); $x++) { 
            
    $pattern = '/(^|[^a-zA-Z0-9#@_\\'\\"])(' . $keywords[$x] . ')([^a-zA-Z0-9#_\\'\\"]|$)/'; 
            
    $code preg_replace($pattern'\\\\1<font color="blue">\\\\2</font>\\\\3'$code); 
        }

        
    $single '/((^|[^(https?|ftp|gopher|irc|news|telnet):])\\/\\/(.*?))([\\r\\n]|$)/is'

        if (
    preg_match_all($single$code$match)) 
        { 
            
    $return preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]); 
            
    $return preg_replace($single,'<font color="green">\\\\1</font>\\\\4',$return); 
            
    $code str_replace($match[0],$return,$code); 
        }

        
    $multi '/(\\/\\*(.*?)(\\*\\/|$))/is'

        if (
    preg_match_all($multi$code$match)) 
        { 
            
    $return preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]); 
            
    $return preg_replace($multi,'<font color="green">\\\\1</font>',$return); 
            
    $code str_replace($match[0],$return,$code); 
        }

        
    $spattern '/(\\&quot;(.*?)[^\\\\\\]&quot;)/is';

        if (
    preg_match_all($spattern$code$match)) 
        { 
            
    $return preg_replace('/<font color=(.*?)>(.*?)<\\/font>/','\\\\2',$match[0]); 
            
    $return preg_replace($spattern,'\\\\1',$return); 
            
    $code str_replace($match[0],$return,$code); 
        }

        
    $code str_replace("&quot;""\\"", $code);
        
    $code = str_replace("<br>", "", str_replace("<br />", "", $code));

        return "
    <blockquote><pre><smallfont>CSharp Code:</smallfont><hr size=1><br />" . $code . "<hr size=1></pre></blockquote>";

    Last edited by Tewl; Mar 25th, 2004 at 12:57 PM.

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