开发者

"globbing" a file getting only those named numericlly?

I would like to use the php function glob() to get those file which follow this pattern:

[arbitrary-text-code开发者_运维百科]-[any number].jpg

I can seem to find the way to do this.. also, is there another way to go about it?

Thanks for the help!


Are the ranges for [any number] arbitrary? If yes, I think your best option is preg_filter():

$text = '.+';
$number = '\d+';
$regex = $text . '-' . $number . '[.]jpg$';

$result = preg_filter('~' . $regex . '~i', '$0', glob('/your/path/*.jpg');

If, however, [any number] has a simple, small range (like [0-9]) GLOB_BRACE is your friend:

$result = glob('/your/path/*-{0,1,2,3,4,5,6,7,8,9}.jpg', GLOB_BRACE);

PS: I haven't tested the following and I'm not sure if it works but it's worth a shot.


If your [any number] is between, lets say, 0 and 99, this might work:

$result = glob('/your/path/*-{0,1,2,3,4,5,6,7,8,9}{,0,1,2,3,4,5,6,7,8,9}.jpg', GLOB_BRACE);

Similarly, for 0 to 999:

$result = glob('/your/path/*-{0,1,2,3,4,5,6,7,8,9}{,0,1,2,3,4,5,6,7,8,9}{,0,1,2,3,4,5,6,7,8,9}.jpg', GLOB_BRACE);

You get the idea. The trick here is the empty option on the latter braces - let me know how it goes.


GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'.


I dont think there is a way to glob a repeated pattern like any number one or more times so at the least youd have to glob something like '*-*[0123456789].jpg' and then preg_grep or something on the array it returns.

I have in the past pulled sfFinder out of Symfony (usage doc here - its outdated but the API hasnt change much) to do things like this so i wouldnt have to code my own solution :-) I assume there is probably something similar in PEAR or Zend Framework though if youre already using one of those.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜