Source Code Syntax Highlighting in PHP
They said programming is an art, and syntax highlighting of source code really makes them look artistic.
I have some little scripts that I would to publish on the net, and I would like to add some colour before posting in my blog. PHP has this function show_source() that can syntax highlight a file, however it only works on PHP files. Instead of writing parsers and generators for all the programming languages known to mankind out there, I opt for something already been written – GNU Enscript.
I know that Enscript is mainly use to generate Postscript files, but in a tool which I’ve used before, ViewCVS, I saw how it calls Enscript to generate syntax highlighted source code listing. It then took me a few minutes to hack up a wrapper for it in PHP…
- Download the source file: syntax_hilight.php
This is an demonstration of how to use it.
<?php
echo syntax_hilight('syntax_hilight.php');
?>
Source Code
<?php
/**
* Syntax hilighting a program source file. It calls enscript(1) to parse and
* insert HTML tags to produce syntax hilighted version of the source. Since
* the version of enscript I have does not support PHP hilighting, I will use
* PHP's show_source() if the source file ends with .php.
*
* @param $filename The filename of the source file to be transformed.
* @return A text string containing syntax hilighting version of the source,
* in HTML.
*/
function syntax_hilight($filename) {
if ((substr($filename, -4) == '.php')) {
ob_start();
show_source($filename);
$buffer = ob_get_contents();
ob_end_clean();
} else {
$argv = '-q -p - -E --language=html --color '.escapeshellcmd($filename);
$buffer = array();
exec("enscript $argv", $buffer);
$buffer = join("\n", $buffer);
$buffer = eregi_replace('^.*<PRE>', '<pre>', $buffer);
$buffer = eregi_replace('</PRE>.*$', '</pre>', $buffer);
}
// Making it XHTML compatible.
$buffer = eregi_replace('<FONT COLOR="', '<span style="color:', $buffer);
$buffer = eregi_replace('</FONT>', '</style>', $buffer);
return $buffer;
}
?>
Updated: This has been superseded by the newer version that also can acts as a WordPress plugin.
Comments
“Now, how does ViewCVS call your script? Do you have doco on that hack?”
ViewCVS does not call this code. This is merely an example of how to perform syntax highlighting of several languages with PHP.
BTW: I am using a similar setup to highlight my code snippets on my site (http://www.designcurve.net/snippets/).
–DesignCurve
Just what I was looking for. I just couldn’t put together all the pieces myself. I too discovered enscript in connection with ViewCVS.
Doesn’t this work if php safe mode is enabled?
(Warning: show_source(): SAFE MODE Restriction in effect.)
The regexp that turns font tags into spans is broken. You close with instead of with .
Neither do you need to use two regexps – of which the second should be an str_replace … but you can do it all with a single regexp. I’ve tried a couple of times to post my version of that bit but the form keeps eating the patterns – presuming thinking they’re HTML. I’ll try again with > and < instead of plain > and < …
$pattern = ‘/<FONT COLOR=”\([^"]*)”>.*?<\/FONT>/’;
$buffer = preg_replace ($pattern, ‘<span style=”color:\1″>\2?<\/span>’, $buffer);
Nice, it works. Go the preview!! :)
Ahem … it also ate some of my post … what I was trying to say earlier was that:
‘You close with </style> instead of </span>’
.. I guess you have something that eats HTML tags on your site? Maybe it would make more sense to just go:
$post = str_replace (‘<’, ‘<’, $post);
$post = str_replace (‘>’, ‘>’, $post);
Just fyi, there’s a color-code / syntax-highlighting text editor for Windows called ConTEXT — although it’s pretty simple (just goes through the syntax and encapsulates words with font-color tags), it features exporting to static HTML, which may be more preferable than always calling a server-intensive dynamic script.
Just my two cents (^_^)
I am having trouble getting the whole enscript to work on my site, half the time it doesn’t even highlight the code correctly. :/
Add a comment
Gravatar is used. Email address is required but will not be displayed. Please keep your comment on topic. No spamming and/or bad language. First time poster will be moderated. Scott reserves the right to delete/edit your comments.

This seems cool. And I was searching for this capability in ViewCVS. Incidentally, http://www.horde.org has Chora CVS viewer, which highlights php syntax. But I am using ViewCVS.
Now, how does ViewCVS call your script? Do you have doco on that hack?