Jump to content

Preg_match doesn't seem to work.


Imad

Recommended Posts

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

# \.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./'

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.