/    Sign up×
Community /Pin to ProfileBookmark

Need help with preg_match

Hey,

I know basically what I have to do to achieve what I want, but preg_match is so confusing with its search criteria and characters. I want to make a loop that reads all the files in a folder (they are all images) and sort them by their initial filename and the number included.

Each folder will have something like this:
large1.jpg
large2.jpg
large3.jpg
thumb1.jpg
thumb2.jpg
thumb3.jpg
main1.jpg
main2.jpg
main3.jpg
original1.jpg
original2.jpg
original3.jpg

I want to sort it something like “original[0-9].jpg” and either put it in array or whatever so I can count the total (like 3 of each) and use each one. I have the right loop I think for the files, but I don’t know how to do the filename filter. Any help would be appreciated. Thanks.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@DARTHTAMPONMay 15.2007 — something like this might lead you in the right direction. Spend a little time researching associated arrays. (note this is untested code)

[code=php]
foreach ($file in dir)
{
#chop off the numbers and .jpg
$filename = @preg_replace('/^(.+)([0-9]{1,}.jpg)$/','\1',$file);
if ($fileCounts[$filename] != null)
{
$fileCounts[$filename]++;
}
else
{
$fileCounts[$filename] = 1;
}
}
[/code]
Copy linkTweet thisAlerts:
@bokehMay 15.2007 — [code=php]if ($fileCounts[$filename] != null)
{
$fileCounts[$filename]++;
}
else
{
$fileCounts[$filename] = 1;
}
}
[/code]
[/QUOTE]
or just:[code=php]@$fileCounts[$filename]++;[/code]
×

Success!

Help @KillaH425 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...