My understanding of regex and working with strings in php is poor at best so my apologies if this is a simple question for most. If I’m honest the searches I have made have confused me more than anything else, hopefully someone here can give me a clear answer.
I simply need to increment the suffix of file names in PHP.
The file names look like the following:
13132432343243432434234_1.jpg
I wish to keep the file name exactly as is, but replace the final ‘_1’ part with ‘_2’, ‘_3’, ‘_4’ etc.
There is no need to check if the file exists, this needs to be a simple string manipulation. If the suffix is _1, then replace it with _2 and if the suffix is _4, then replace it with _5 etc.
Thanks in advance.
Something along these lines should do it:
The regex matches “any number preceded by a
_and followed by a.“, the callback returns this number incremented by one.