I saw the same problem and started tinkering with it without realizing you'd already started, sorry.
I found a nice public domain PHP lexer class on Sourceforge and thought it looked like a lazy man's way of solving the problem (although it's probably overkill). After seeing that you'd started on the problem, I thought about stopping but then decided to push on just to improve my PHP skills.
The code is attached, and there's a demo at:
Right now I'm not planning on developing it any further (you beat me to it, fair and square ), but I thought others might find the code useful (maybe for other applications in Moodle, even).
It's really quite nifty to see Bison/Yacc/lex/flex-like functionality in PHP. The more I use PHP the more I like it.
I expect your hand-coded parser has better performance. OTOH, the lexer method may be easier to extend.