Imad Posted July 19, 2008 Share Posted July 19, 2008 Hi Guys, I'm trying to achieve a little script to scan a directory and bring back php files only, here's what I have so far: if ($handle = opendir('versions')) { echo "Files:\n"; while (false !== (preg_match("/\.php([^\.php]+)$/", $file) && $file = readdir($handle))) { echo "$file\n"; } return $file; } It doesn't seem to return any php files in the directory. Any ideas why? Best Regards. Link to comment https://forums.phpfreaks.com/topic/115637-preg_match-doesnt-seem-to-work/ Share on other sites More sharing options...
unkwntech Posted July 19, 2008 Share Posted July 19, 2008 # \.php([^\.php]+)$ # # Match the character “.” literally «\.» # Match the characters “php” literally «php» # Match the regular expression below and capture its match into backreference number 1 «([^\.php]+)» //what is the purpose for this # Match a single character NOT present in the list below «[^\.php]+» # Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» # A . character «\.» # One of the characters “ph” «php» # Assert position at the end of the string (or before the line break at the end of the string, if any) «$» If you just trying to check if the file has a .php extension then use this: '/\.php./' Link to comment https://forums.phpfreaks.com/topic/115637-preg_match-doesnt-seem-to-work/#findComment-594453 Share on other sites More sharing options...
DarkWater Posted July 19, 2008 Share Posted July 19, 2008 How about you use: foreach (glob('somedir/*.php') as $filename) { echo filesize($filename) . "<br />"; } Link to comment https://forums.phpfreaks.com/topic/115637-preg_match-doesnt-seem-to-work/#findComment-594463 Share on other sites More sharing options...
Imad Posted July 19, 2008 Author Share Posted July 19, 2008 Thanks for your help guys, I figured I'd put the substr to use and created this: $directory = @opendir("files"); if($directory) { while($modules = @readdir($directory)) { //file extension || substr: part of file || strrchr: end of char $extention = substr(strrchr($files, "."), 1); //Check to see if php is after the period if($extention == "php") { $files_list[] = $files; } } sort($files_list); } @closedir($directory); return $files_list; } Link to comment https://forums.phpfreaks.com/topic/115637-preg_match-doesnt-seem-to-work/#findComment-594465 Share on other sites More sharing options...
DarkWater Posted July 19, 2008 Share Posted July 19, 2008 Just use glob().... foreach (glob('versions/*.php') as $filename) { $module_list[] = $filename; include("versions/$filename"); } Link to comment https://forums.phpfreaks.com/topic/115637-preg_match-doesnt-seem-to-work/#findComment-594467 Share on other sites More sharing options...
Imad Posted July 19, 2008 Author Share Posted July 19, 2008 Just use glob().... foreach (glob('versions/*.php') as $filename) { $module_list[] = $filename; include("versions/$filename"); } Will do then. Link to comment https://forums.phpfreaks.com/topic/115637-preg_match-doesnt-seem-to-work/#findComment-594470 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.