Edward Tanguay
Edward Tanguay

Reputation: 193452

PHP function which does syntax color parsing for multiple languages?

I'm doing a PHP site which displays code examples in various languages (C#, PHP, Perl, Ruby, etc.). Are there any PHP functions which add syntax coloring for these and other languages?

If not, I would at least like to find that one built-in PHP function which does syntax coloring for PHP code, can't find it anymore. Thanks.

Upvotes: 0

Views: 578

Answers (4)

Tom Haigh
Tom Haigh

Reputation: 57845

As already mentioned Google prettify.js is really good, but if you want to do it on the server in PHP you could try looking at Pear Text Highlighter

Upvotes: 0

Paige Ruten
Paige Ruten

Reputation: 176803

For highlighting PHP use highlight_string(). (This may work OK with other languages as well.)

Edit: This function requires that the string start with the PHP opening 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 regex to strip out the starting and ending tags that I had added in. This method has worked pretty well for highlighting C/C++, Scheme, and Java (and PHP that doesn't have the <?php ?> tags.)

Upvotes: 1

Tom Ritter
Tom Ritter

Reputation: 101400

You'd probably be better off formatting it in javascript actually. There are a few mature javascript syntax colors.

Upvotes: 2

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 828012

Why not do the syntax coloration in the client side?

Use prettify.js, its really versatile, Google Code and StackOverflow use it!

Check the test page for the supported languages.

Upvotes: 4

Related Questions