# How would I write the following in PHP using wildcards?

**URL:** <https://boards.straightdope.com/t/how-would-i-write-the-following-in-php-using-wildcards/614752>\
**Category:** Factual Questions\
**Created:** [March 4, 2012, 9:22pm UTC](https://boards.straightdope.com/t/how-would-i-write-the-following-in-php-using-wildcards/614752 "2012-03-04T21:22:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cagey\_Drifter](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Cagey\_Drifter](https://boards.straightdope.com/u/Cagey_Drifter)\
**Post date:** [March 4, 2012, 9:22pm UTC](https://boards.straightdope.com/t/how-would-i-write-the-following-in-php-using-wildcards/614752/1 "2012-03-04T21:22:54Z")

</div>

Hopefully this makes sense: Using PHP, I want to use wildcards to create an expression that refers to any child folder (subfolder) relative to the current folder. How would I do that?

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [March 4, 2012, 10:49pm UTC](https://boards.straightdope.com/t/how-would-i-write-the-following-in-php-using-wildcards/614752/2 "2012-03-04T22:49:36Z")

</div>

It depends on what you’re trying to do with them (get all files from within them, list them as strings, etc.), but a starting place might be the [RecursiveDirectoryIterator](http://php.net/manual/en/class.recursivedirectoryiterator.php).

Wildcards in most languages are independent of file system structures, so it’s kind of hard to answer that without knowing the context of your code.

---

<div class="post-metadata">

**Author:** ![Cagey\_Drifter](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Cagey\_Drifter](https://boards.straightdope.com/u/Cagey_Drifter)\
**Post date:** [March 4, 2012, 11:13pm UTC](https://boards.straightdope.com/t/how-would-i-write-the-following-in-php-using-wildcards/614752/3 "2012-03-04T23:13:44Z")

</div>

> [@Reply](#):
>
> It depends on what you’re trying to do with them (get all files from within them, list them as strings, etc.), but a starting place might be the [RecursiveDirectoryIterator](http://php.net/manual/en/class.recursivedirectoryiterator.php).
> 
> Wildcards in most languages are independent of file system structures, so it’s kind of hard to answer that without knowing the context of your code.

Thanks for the link, but what I hoping to get was a [regular expression](http://en.wikipedia.org/wiki/Regular_expression) that could be used in PHP.

---

<div class="post-metadata">

**Author:** ![tellyworth](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tellyworth](https://boards.straightdope.com/u/tellyworth)\
**Post date:** [March 5, 2012, 12:07am UTC](https://boards.straightdope.com/t/how-would-i-write-the-following-in-php-using-wildcards/614752/4 "2012-03-05T00:07:10Z")

</div>

[glob()](http://php.net/manual/en/function.glob.php) works with wildcards. This will return an array of the names of immediate subdirectories:

> [@](#):
>
> $names = glob( ‘\*’, GLOB\_ONLYDIR);

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [March 5, 2012, 12:37am UTC](https://boards.straightdope.com/t/how-would-i-write-the-following-in-php-using-wildcards/614752/5 "2012-03-05T00:37:32Z")

</div>

> [@Cagey\_Drifter](#):
>
> Thanks for the link, but what I hoping to get was a [regular expression](http://en.wikipedia.org/wiki/Regular_expression) that could be used in PHP.

Well, PHP does support [regular expressions](http://php.net/manual/en/function.preg-match.php), but typically regular expressions by themselves have no awareness of folder structure or even a way to interact with the filesystem.

Regular expressions interact with strings. Do you have a list of folders and subfolders that you want to do a regex match against?
