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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.