The Little Guy Posted July 18, 2010 Share Posted July 18, 2010 is there a php function like glob that uses regular expressions or will I have to make my own? I want to get all files in a directory that are in the following format: bg#.jpg # is a number 1, 2, 3, 4, etc. Link to comment https://forums.phpfreaks.com/topic/208068-glob-regex/ Share on other sites More sharing options...
marcus Posted July 18, 2010 Share Posted July 18, 2010 glob works just like that. I have the following files test/bg1.txt, test/bg2.txt, test/bg3.txt, test/bg4.txt, test/cat.txt and test/africa.txt Using the following code: <?php print_r(glob('test/bg*.txt')); ?> I get the following results: Array ( [0] => test/bg1.txt [1] => test/bg2.txt [2] => test/bg3.txt [3] => test/bg4.txt ) Obviously you would just change .txt to .jpg Link to comment https://forums.phpfreaks.com/topic/208068-glob-regex/#findComment-1087630 Share on other sites More sharing options...
marcus Posted July 18, 2010 Share Posted July 18, 2010 Actually, since you only want numbers after bg, my previous post would do anything bg* really. Here is the modified version. <?php print_r(glob('test/bg{[0-9]*}.txt',GLOB_BRACE)); ?> Returns Array ( [0] => test/bg1.txt [1] => test/bg12.txt [2] => test/bg2.txt [3] => test/bg3.txt [4] => test/bg4.txt ) Link to comment https://forums.phpfreaks.com/topic/208068-glob-regex/#findComment-1087632 Share on other sites More sharing options...
The Little Guy Posted July 18, 2010 Author Share Posted July 18, 2010 alright cool! Thanks Link to comment https://forums.phpfreaks.com/topic/208068-glob-regex/#findComment-1087634 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.