TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-08-2008, 10:44 PM   #1 (permalink)
The Contributor
 
Join Date: Dec 2007
Location: Florida
Posts: 73
Thanks: 12
danielneri is on a distinguished road
Default 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
Send a message via AIM to danielneri
danielneri is offline  
Reply With Quote
Old 01-08-2008, 10:56 PM   #2 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

These will help:
PHP: highlight_string - Manual
PHP: highlight_file - Manual
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-08-2008, 10:56 PM   #3 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

highlight_string() will highlight php code for you, though I'm not entirely certain whether it will automatically display it to the screen.
TlcAndres is offline  
Reply With Quote
Old 01-08-2008, 10:58 PM   #4 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

It will print unless you say highlight_string($s, false) at which point it is returned, not echoed.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-08-2008, 11:18 PM   #5 (permalink)
The Contributor
 
Join Date: Dec 2007
Location: Florida
Posts: 73
Thanks: 12
danielneri is on a distinguished road
Default

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?
Send a message via AIM to danielneri
danielneri is offline  
Reply With Quote
Old 01-08-2008, 11:22 PM   #6 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

You'd have to do a preg_replace using the callback function. I don't remember how specifically, but the manual is quite helpful.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-08-2008, 11:37 PM   #7 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

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

Last edited by Kalle : 01-09-2008 at 01:58 PM.
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 01-09-2008, 02:02 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:42 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design