I made a couple of small improvements to my code a few minutes after posting:
- removed an unnecessary assignment to $split;
- moved the other $split assignment into the else clause.
Your code is based on the original. Sorry I didn't flag the update, I didn't think anyone would have already d/led the code so quickly!