PHP function that does color parsing for multiple languages?
I am making a PHP site that displays code examples in different languages (C #, PHP, Perl, Ruby, etc.). Are there any PHP functions that add syntax coloring for these and other languages?
If not, I'd like to at least find that one built-in PHP function that does syntax coloring for PHP code can't find it anymore. Thank.
source to share
Why not do the client side syntax coloring?
Use prettify.js , its really versatile, Google Code and StackOverflow use it!
Check the test page for supported languages.
source to share
Use highlight_string () for PHP highlighting . (This may work fine with other languages as well.)
Edit: This function requires the line to start with a PHP open tag. What I did on my site to get around this was I passed something like "<?php\n$code\n?>"
to the highlight_string () function and then used a regex to highlight the start and end tags that I added. This method worked really well for highlighting C / C ++, Scheme and Java (and PHP which have no tags <?php ?>
).
source to share
As mentioned, Google prettify.js is really good, but if you want to do it on the server in PHP you can try looking at Pear Text Highlighter
source to share