March 30, 2004

PHP Pigmentator

The following was tweaked for PHP 5 with this guy's help.

PHP's highlight_file function is a quick and dirty way to achieve basic syntax coloring, which can make your code a lot easier to read. The dirty part is its egregious use of icky invalid (at least for XHTML) font tags.

Some of the user contributed notes explain various methods for fixing this. I've cobbled together a few of these and tweaked them to get rid of the non-breaking spaces, which seem to make a mess of copying and pasting the code. Us lazy people hate to type when we don't have to. The downside is that we have to use a <pre> tag, so it won't wrap nicely. If you don't like really wide pages, then you'd better hard wrap your code before you feed it to the colorizer.

If you feed this beast some code, it will spit it back to you in two versions. First, colorized and rendered in the browser, and then as raw XHTML in a text box, ready to be copied and pasted into a page. At the bottom of the page is the CSS style declaration used to colorize the spans produced by this script.

CSS style declaration

div.phpcode span.html { color: #000; }
div.phpcode span.comment { color: #f80; }
div.phpcode span.default { color: #00b; }
div.phpcode span.keyword { color: #070; }
div.phpcode span.string { color: #d00; }

Posted by Hal Eckhart at March 30, 2004 04:39 PM | TrackBack

It's a really nice script, keeps me showing ugly plain colourless PHP code to others! It's bit too much work doing this manually... ;)

Thank you!

Posted by: scorch at November 20, 2007 03:17 AM

First, thanks for the script! After some tweaking (see below), this absolutely saved me. I had an unclosed string that I absolutely COULD NOT find in my program, and other tools weren't helping me.

Two issues I found, though.

For one, your program isn't spitting out the proper code. It's *definitely* not using the source you list on the previous post. Rather than spitting out, say, <span class="string">, it pops out <span style="color: string">. Fixing it in my outputted code was a simple matter of a few find-and-replaces, but I'm not sure how this could happen in the first place. I was about to give up on your script before I was lucky enough to notice this!

For two, while the colors are nicely readable, they aren't *nearly* distinguishable enough. It might just be because I have a touch of colorblindness, but while the green and red are easy to tell apart in text, they're impossible when they're coloring, say, a lone } on a line. Again, I was about to give up on your script (and beg for help) before I changed the styles and got more distinguishable colors. I simply changed all of them to their most extreme variants: #f00, #0f0, and #00f. I also ran a { font-weight: bold } across everyting in the wrapper div, so that the color had more pixels to act on.

After fixing and tweaking, though, your script was wonderful. I found my problem right away once I could clearly see the difference between a green } and red }.

Posted by: Xanthir, FCD at March 14, 2008 08:38 PM


Wrapping my email address into a mailto is NOT cool. The standard method is to wrap the commenter's WEBSITE in a link around their name, and save the email privately so you can converse with them if you want. You're just inviting harvesters onto your site.

Sure, I use a mail service with good spam blocking. But that's irrelevant. This sort of thing is just rude to your commenters.

Posted by: Xanthir, FCD at March 14, 2008 08:46 PM

Heh, okay, after actually tweaking the source for myself, I found the problem. More recent versions of php (including, apparently, the one you're running) no longer use <font> tags in the highlight_string function. Instead, they use <span style=""> tags. Identical in everything but name. You just need to update the replacement array slightly to get it to output correctly.

Change the line that reads: ' to 'style="color: ',, and the corresponding line that says ' with 'class="',. As well, remove the very next line, the one that replace </font> with </span, and it's corresponding line in the second array.

As noted previously, I also changed the CSS rules to use the most extreme versions of the colors. Slightly less readable, but much more noticeable, especially for the colorblind like me (my coworker says she can tell the difference between your colors just fine).

(Highlighted) source code here.

Posted by: Xanthir, FCD at March 14, 2008 09:41 PM

Or, since links are stripped, here:

Posted by: Xanthir, FCD at March 14, 2008 09:41 PM
Post a comment
spam and annoying comments will be deleted

Remember personal info?

enter this number security code in the box below (prevents comment spam)