I'm running into a bit of trouble with this code I recently made, in theory it would have worked fine.

I need help on how I can get the following done:
I need to match a changing string and then replace it based on how many times it was previously replaced.
I've tried many variations of this, here is my most recent:

Well the while and preg_match cases seem to work fine but I'm not seeing any filtered results based on how many times that it has been matched.
I can't think of any other ways to do it, I need a little extra brain power here.