fnmatch()
fnmatch() — Match filename against a pattern.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
fnmatch() checks if the passed filename would match the given shell wildcard pattern.
Signature
fnmatch(string $pattern, string $filename, int $flags = 0): bool
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $pattern | string | The pattern to match against. Usually, the pattern will contain wildcards like '?' and '*'. Wildcards to be used in pattern parameter Wildcard Description ? Question mark will match any single character. For example, pattern "file?.txt" will match "file1.txt" and "fileA.txt", but will not match "file10.txt". * Asterisk will match zero or more characters. For example, pattern "foo*.xml" will match "foo.xml" and ... |
| $filename | string | The tested string. This function is especially useful for filenames, but may also be used on regular strings. |
| $flags | int | The value of flags can be any combination of the following flags, joined with the binary OR (|) operator. A list of possible flags for fnmatch() Flag Description FNM_NOESCAPE Disable backslash escaping. FNM_PATHNAME Slash in string only matches slash in the given pattern. FNM_PERIOD Leading period in string must be exactly matched by period in the given pattern. FNM_CASEFOLD Caseless match. Part of the GNU extension. |
Return value
Returns true if there is a match, false otherwise.
Example
A runnable snippet:
if (fnmatch("*gr[ae]y", $color)) { echo "some form of gray ..."; }
See also
glob(), preg_match(), sscanf(), printf(), sprintf()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page fnmatch() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.