PHP brush for SyntaxHighlighter

Alex Gorbatchev's SyntaxHighlighter is a well-written bundle which enables syntax highlighting of code via JavaScript. More than twenty languages are supported "out of the box", and brushes (JavaScript files containing language-specific regular expressions) can be created to support additional languages.

Unfortunately, however, several of the brushes that come bundled with SyntaxHighlighter are far from perfect. Have a look at the bundled PHP brush in action below.

Screenshot of PHP code highlighted by SyntaxHighlighter's PHP brush
Screenshot of bundled PHP brush in action

I would give this brush 6/10. Here are its deficiencies, as I see them:

  • PHP opening and closing tags are not captured
  • Variables within double-quoted strings are not captured
  • Numerical values are not captured
  • Only a fraction of PHP's function names are recognized
  • Custom function names are not captured

I've created an improved PHP brush which remedies these deficiencies. It uses the following class names: phptag for opening and closing PHP tags, including short tags; comments for both single- and multi-line comments; string for both single- and double-quoted strings; varinstr for variables within double-quoted strings; numval for numerical values; function for documented PHP functions; custfunc for custom (user-defined) functions; and constant, keyword, and variable for exactly what you'd expect.

Update —

Until this point I have had a class name added to each div.syntaxhighlighter element to allow code to be coloured in a language-specific manner. This approach fails, however, when a highlighted block features two languages (PHP and HTML, for example). CSS selectors cannot differentiate between two code elements with the same class name in the same div.syntaxhighlighter element.

Each language, therefore, needs to use its own class names. It is easy to differentiate code.php-comment from code.xml-comment, allowing PHP comments to be styled differently from XML comments if desired.

The updated class names are php-tag, php-comment, php-string, php-varinstr, php-numval, php-function, php-custfunc, php-constant, php-keyword, and php-variable.

<?php

'single-quoted string';

"double-quoted string";

// single-quoted string with literal dollar sign
'fruit smoothie: $5.50';

// double-quoted string containing a variable
"fruit smoothie: $cost";

// numerical value
$cost = 5.50;

// a few common function calls
empty($variable);
isset($variable);
strlen($variable);
strrev($variable);

// a call to a custom function
is_ready_to_order($customer);

?>

Live rendering of improved PHP brush

Comments

Very nice improvements. Thanks!

mattyoung

I've added a reference to your improved PHP brush on my overview of "all" SyntaxHighlighter brushes. I'm working on a bundled download of all brushes (esp. with all the ones not included in the standard distribution). Would it be ok if I added your brush in the download, with due credit of course?

That is absolutely fine, Abel. Thanks for taking the time to compile a list of brushes – it'll be my first port of call for non-bundled brushes in future. You're welcome to include my AppleScript brush as well, if you'd like to do so.

This doesn't seem to work at all in the latest version of SyntaxHighlighter. I put your example into 'test.html' and confirmed that the latest version still doesn't recognize everything. Then I put your 'shBrushPhp.js' into the 'scripts' directory (overwriting the original file) and reloaded the page. The result is plain text. I've reverted back to the original brush.

Edit: After looking at your code for a bit, it looks like you are putting styles in with a 'php-' prefix. That's fine, but you should also include one of the default classes too (without the prefix) and supply some default CSS content for those who want to customize the display.

Not Working

I've updated the improved PHP brush {{ filesize }} to include standard SyntaxHighlighter class names (in addition to the existing prefixed class names). This enables the brush to be used as is with the default style sheet.

Improved PHP brush with default styling
Improved PHP brush used in conjunction with default style sheet

Wow! That was fast! Works like a charm even in IE6. Thanks!

Works Now

I'm always surprised when anything works in IE6. >.<

I'm getting a "403 Forbidden" error whenever I try to access the JavaScript file.

Shauna

I'm sorry about that, Shauna. I uploaded a fresh copy of the file and the link now works correctly.

Respond