Oliver Spryn
Oliver Spryn

Reputation: 17368

PHP Get File By Regexp

Is there a way that I can have PHP get a file by a Regexp search? I know how to open and read a directory no problem, but finding a particular file is the problem.

Here is an example, I have a logo which may end in any of the following extensions: .bmp, .png, .jpg, .jpeg, or .gif. However, the name of the file will always be icon.someext.

$filesDirectory = scandir("/files");

foreach($filesDirectory as $file) {
    [Regexp find logo here] ? $logo = $file : null;
}

echo $logo; // Returns something like logo.png

Thank you for your time.

Upvotes: 1

Views: 5014

Answers (3)

Michael Brevig
Michael Brevig

Reputation: 109

Try using preg_match() https://www.php.net/manual/en/function.preg-match.php

If you need help using it, let me know.

Upvotes: -1

Floern
Floern

Reputation: 33904

You can use glob with wildcard and GLOB_BRACE:

$files = glob("icon.{png,jpg,jpeg,gif,bmp}", GLOB_BRACE);

Upvotes: 10

Marc B
Marc B

Reputation: 360782

$files = glob("icon.*");

works pretty much like wildcarding on a unix command line does.

Upvotes: 2

Related Questions