tags:

views:

25

answers:

1

My OS environment is linux.

I have a php file in this folder - /var/www/html (say... test.php)

I also have a folder called DATA_SOMENUMBERHERE in the same directory.

Inside test.php, I need to find out the SOMENUMBEREHERE.

In other words, find the folder whose name starts with DATA_ and get the remainder part of the string.

One way to do it is, get all folder names in that directory and loop through them and find out.

But is there another way? Can I pass some kind of regexp to some file system function call so that I get all folder names matching that pattern?

A single function call will be ideal. But if none are available, I am okay with looping through.

Safe Assumptions:

  • There will always be a folder that starts with DATA_

  • There will be only one folder that starts with DATA_

  • SOMENUMBERHERE will keep changing.

+4  A: 

Take a look at glob() which returns an array of files/directories that match a pattern. The GLOB_ONLYDIR option allows you to only search for directories that match the pattern. So for example:

$dirs = glob('DATA_*', GLOB_ONLYDIR);
//$dirs[0] now holds the name of the directory.

If you are very sure about the fact that there will be a directory there you can use also use list to shorten the code slightly.

list($dir) = glob('DATA_*', GLOB_ONLYDIR);
//$dir now holds the name of the directory.
Yacoby
Hey! :) two for one... thanks for both glob() and list()
Senthil