You need to make that match-all non-greedy, or you’re going to consume the entire string for a single match. (Also… if by definition it’s a string with multiple matches, you’re not going to be able to ^ and $… is this a multiline string?)
\b is for a word boundary. So the first is one is either for the start of the whole string, or a space, while the second one is for the end of the string, or a space.
I’ve also changed .* to [^\s]+? to only match anything that isn’t a space. Otherwise it would combine files if one of them wasn’t a GIF.
Well for starters, try using the new pattern rpkamp’s given you.
You can also somewhat simplify your code with a foreach. It’ll help you avoid the other problem you’ve got - putting array markers inside a string doesnt work without help.
If you want to stick with the for loop, and keep the variable inside the string, you’ll need to add some curly braces so the string knows where the variable reference ends, and the string begins again.
It’s been a while since I did complex string insertion, but i believe it would have to be