My regex is not very good yet and I'm having trouble making yours work within $regex() for testing (for example, I'm not sure what \54 is doing), but basically I'd just make it greedy so it will fill multiple $regml()'s with all results and then loop through until $regml(0) and create the output that way. If there's a better way, I'm not sure.