![]() |
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 :-) |
|
highlight_string() will highlight php code for you, though I'm not entirely certain whether it will automatically display it to the screen.
|
It will print unless you say
highlight_string($s, false) at which point it is returned, not echoed. |
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? |
You'd have to do a preg_replace using the callback function. I don't remember how specifically, but the manual is quite helpful.
|
I wrote this code a while ago which you might find useful:
PHP Code:
$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 |
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