morrism35 Posted October 31, 2015 Share Posted October 31, 2015 Hello everyone, I'm trying to find all the files in my image folder and copy them into an array. The files have to start with "download" and have a "jpg, png, or gif extention". My code is listed below. I verified the code is correct till the foreach statement that does output the $entry array, however something is wrong when i use my preg_grep. It seems like it is not finding the files. I know their are other ways but my teacher wants me to use the pattern match and really in this course we haven't learned any of the advanced techniques that I found in my web searches on this issue. Thank you. $dir = "Images"; $DirEntries = scandir($dir, 1); foreach($DirEntries as $Entry){ $template=preg_grep('/^dog\d+\.(gif|jpg|png)$/', $Entry); echo $template; } Quote Link to comment Share on other sites More sharing options...
Barand Posted October 31, 2015 Share Posted October 31, 2015 Take a look at the glob function Quote Link to comment Share on other sites More sharing options...
0x00 Posted October 31, 2015 Share Posted October 31, 2015 If your string contains just the filename, then you want that character that matches the start of the string, then your string "download", then any, then the three extensions in or brackets followed by the end of line character, simples. Quote Link to comment Share on other sites More sharing options...
morrism35 Posted November 1, 2015 Author Share Posted November 1, 2015 So to find all files with the name download and that are jpg, png, or gif this is the pattern? I've tried this and it is not finding it. $template=preg_grep('/^d\download\./(gif|jpg|png)$/, $Entry'); Quote Link to comment Share on other sites More sharing options...
Barand Posted November 1, 2015 Share Posted November 1, 2015 $imagefiles = glob("images/download*.{png,jpg,gif}", GLOB_BRACE); echo '<pre>',print_r($imagefiles, true),'</pre>'; Quote Link to comment Share on other sites More sharing options...
Solution morrism35 Posted November 1, 2015 Author Solution Share Posted November 1, 2015 thank you I was able to use the preg_grep with a pattern similiar to what you have Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.