You could try adapting the explode function.
It will split a string into an array based on a character used as a "separator." Here is the syntax:
explode(separator, string [,limit]);
"limit" is an optional numeric value limiting the number of elements to split the string into. Don't know if this will help, but maybe you can work with it. Also, there is an "implode" function to reverse the process.