Hello. Sorry to make my first post on this forum a request for help, but I've tried elsewhere with this problem and never really got an answer.
I have the following code:
<?php
function repdir($dirs, $images, $org) {
$dirs = str_replace('[','',$dirs);
$dirs = explode('-',str_replace(']','',$dirs));
$images = str_replace('[','',$images);
$images = explode('-',str_replace(']','',$images));
$ret = array();
for ($i=$dirs[0];$i<=$dirs[1];$i++) {
$zero = (substr($images[1],0,1) == '0' ? '0' : '');
for ($ii=$images[0];$ii<=$images[1];$ii++) {
$ret[] = $org[1].$i.$org[3].$zero.(int)$ii.$org[5];
}
}
return $ret;
}
$results = array();
$scan = "http://www.foo.com/dir[1-5]/image[01-05].jpg";
ereg("(.*)(\[.*\])(.*)(\[.*\])(.*)", $scan, $reg);
$results = repdir($reg[2],$reg[4], $reg);
foreach($results as $key => $value) {
print "$key: $value<br>";
}
?>
... which I use on my site to allow users to list images and files stored as sequentially named files on the server. This is useful, for example, when large downloads are split into several sequentially named files, or for a series of screenshots, etc.
The above code (should) produce a list like:
http://www.foo.com/dir1/image01.jpg
http://www.foo.com/dir1/image02.jpg
http://www.foo.com/dir1/image03.jpg
.... up to
http://www.foo.com/dir5/image04.jpg
http://www.foo.com/dir5/image05.jpg
This code works, but only allows for two ranges of numbers; one for the directory and one for the filename. Can anybody suggest a way (or ideally, post some code) that would handle any number of ranges in a string, at any position, with or without leading zeroes?
My original idea was to have a function that I could call recursively from within itself, looping until it ran out of ranges to expand. This (literally) gave me a headache, because I'm far from fluent in regular expressions.
Any help would be infinitely appreciated. This has been annoying me for weeks.