TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Syntax Highlighting? (http://www.talkphp.com/general/1908-syntax-highlighting.html)

danielneri 01-08-2008 10:44 PM

Syntax Highlighting?
 
I need to highlight large chunks of PHP code dynamically...

Is there anything out there that will do it easily?

The code right now is in bbcode format, so some kind of bbcode parser that actually highlights the code would be nice :-)

RobertK 01-08-2008 10:56 PM

These will help:
PHP: highlight_string - Manual
PHP: highlight_file - Manual

TlcAndres 01-08-2008 10:56 PM

highlight_string() will highlight php code for you, though I'm not entirely certain whether it will automatically display it to the screen.

RobertK 01-08-2008 10:58 PM

It will print unless you say highlight_string($s, false) at which point it is returned, not echoed.

danielneri 01-08-2008 11:18 PM

Alright so how would I go about parsing the bbcode?

I did some preg_match_all and preg_replace stuff but it didn't quite work out for some reason...

Any pointers?

RobertK 01-08-2008 11:22 PM

You'd have to do a preg_replace using the callback function. I don't remember how specifically, but the manual is quite helpful.

Kalle 01-08-2008 11:37 PM

I wrote this code a while ago which you might find useful:

PHP Code:

<?php
    
function bbcode_phphighlight($info)
    {
        if(
is_array($info) && !empty($info[1]))
        {
            if(!@
ini_get('safe_mode'))
            {
                
$info[1] = @highlight_string($info[1], true);
            }
            else
            {
                
$temp = @tempnam(@ini_get('session.save_path'), 'phphighlightcache');
                
$fp = @fopen($temp'w');

                if(!
$fp)
                {
                    return(
$info[1]);
                }

                
fwrite($fp$info[1]);
                
fclose($fp);

                
$info[1] = show_source($temptrue);

                @
unlink($temp);
            }

            
/**
             * For PHP4 to make XHTML support
             */
            
$info[1] = str_replace(Array(
                            
'<font color="'
                            
'">'
                            
'</font>'
                            
), 
                        Array(
                            
'<span style="color: '
                            
';">'
                            
'</span>'
                            
), 
                        
$info[1]
                        );

            
/**
              * Style the output
             */
            
return(str_replace('<code>''<code style="font: 11px Monaco, Courier, Monospace;">'$info[1]));
        }

        return(
$info[2]);
    }

    
$string "[php]<?php echo('Hello World'); ?>

";
$string = preg_replace_callback('#\[phpcode\](.*?)\[/phpcode\]#i', 'bbcode_phphighlight', $string);

echo($string);
?>[/php]


This should work under whatever options your php setup should have (running in safe mode or not). It will also convert old sloppy PHP4 highlights to nicely XHTML valid tags.

If you're looking into making your own syntax highlighting then I would really recommed you using the tokenizer extension avaible from:
PHP: Tokenizer - Manual

Kalle

Salathe 01-09-2008 02:02 PM

If you're also looking to highlight other languages then you won't go far wrong with GeSHi - Generic Syntax Highlighter (which is what we use here at TalkPHP to beautify the [highlight=lang] blocks in posts. :-)


All times are GMT. The time now is 07:25 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0