I’m working on a open-source plugin for WordPress and frankly facing an odd issue.
Consider the following filenames:
/wp-content/uploads/buddha_-800x600-2-800x600.jpg
/wp-content/uploads/cutlery-tray-800x600-2-800x600.jpeg
/wp-content/uploads/custommade-wallet-800x600-2-800x600.jpeg
/wp-content/uploads/UI-paths-800x800-1.jpg
The current regex I have:
(-[0-9]{1,4}x[0-9]{1,4}){1}
This will remove both matches from the filename, for example buddha_-800×600-2-800×600.jpg will become buddha_-2.jpg which is invalid.
I have tried a variety of regex:
.*(-d{1,4}xd{1,4}) // will trip out everything
(-d{1,4}xd{1,4}){1}|.*(-d{1,4}xd{1,4}){1} // same as above
(-d{1,4}xd{1,4}){1}|(-d{1,4}xd{1,4}){1} // will strip out all size matches
Unfortunately my knowledge with regex is quite limited, can someone advise how to achieve the goal please?
The goal is to remove only what is relevant, which would result in:
/wp-content/uploads/buddha_-800x600-2.jpg
/wp-content/uploads/cutlery-tray-800x600-2.jpeg
/wp-content/uploads/custommade-wallet-800x600-2.jpeg
/wp-content/uploads/UI-paths-1.jpg
Much appreciated!
3
Answers
I would try something like this. You can test it yourself. Here is the code:
It checks for ending -(number)x(number)(dot)(extension)
This is a clear case of « Match the rejection, revert the match ».
So, you just have to think about the pattern you are searching to remove:
which is simply (much condensed):
The next step is to build the groups extractor:
We added the extension of the file as a suffix for the extract.
The rejection of the "x" char is a (bad…) trick to ensure the match of the last size only. It won’t work in the case of an alphanumeric suffix between the size and the extension (
toto-800x1024-ex.jpg
for instance).And then, the replacement string:
For clarity of course, we are only working on a successfully extracted filename. But if you want to treat the whole string, the pattern becames:
If you want to split the filename and the folder name:
You can use a capture group with a backreference to match strings where there are 2 of the same parts and replace that with a single part.
Or match the dimensions to be removed.
(
Capture group 1(-d+xd+)
Capture group 2, match-
1+ digitsx
and 1+ digits-d+
Match – and 1+ digits)2
Close group 2 followed by a backreference to what is captured in grouip 1|
Or-d+xd+
Match the dimensions formatRegex demo | Php demo
For example
Output