Thanks for the clue. In the end, the trim needed to go onto the final element of the array immediately after the explode, rather than before the implode.

Now to turn the whole sorry mess into a function. But that will have to wait until tomorrow.

matigo.ca.