Difference between revisions of "PHP/file/glob"
Jump to navigation
Jump to search
Line 6: | Line 6: | ||
==Functions== | ==Functions== | ||
* [https://www.php.net/manual/en/function.fnmatch {{fmt/code|fnmatch()}}] reports whether the given filespec string matches the given wildcard string | * [https://www.php.net/manual/en/function.fnmatch {{fmt/code|fnmatch()}}] reports whether the given filespec string matches the given wildcard string | ||
+ | ** Note that this does appear to work as expected for filespecs (not just filenames): | ||
+ | php > echo "MATCH: [".fnmatch('/*test/*thing','/sometest/something')."]\n"; | ||
+ | MATCH: [1] | ||
+ | php > echo "MATCH: [".fnmatch('/*test/*thing','/sometest/somethong')."]\n"; | ||
+ | MATCH: [] | ||
+ | php > echo "MATCH: [".fnmatch('/*test/','/sometest/somethong')."]\n"; | ||
+ | MATCH: [] | ||
+ | php > echo "MATCH: [".fnmatch('/*test/*','/sometest/somethong')."]\n"; | ||
+ | MATCH: [1] | ||
* [https://www.php.net/manual/en/function.glob.php {{fmt/code|glob()}}] accepts a [[filespec]] that includes a [[file mask]], and returns a list of matching [[filespec]]s. | * [https://www.php.net/manual/en/function.glob.php {{fmt/code|glob()}}] accepts a [[filespec]] that includes a [[file mask]], and returns a list of matching [[filespec]]s. | ||
** A file mask in this context can be e.g. {{fmt/code|*.php}} or {{fmt/code|/home/woozle/*.php}} but not {{fmt/code|~/*.php}}. | ** A file mask in this context can be e.g. {{fmt/code|*.php}} or {{fmt/code|/home/woozle/*.php}} but not {{fmt/code|~/*.php}}. |
Latest revision as of 14:46, 24 August 2022
PHP filespec wildcard matching & searching aka "globbing"
|
About
PHP can do glob-based directory listings and can also check a given filename string against a given glob string.
See also: globbing
Functions
- «fnmatch()» reports whether the given filespec string matches the given wildcard string
- Note that this does appear to work as expected for filespecs (not just filenames):
php > echo "MATCH: [".fnmatch('/*test/*thing','/sometest/something')."]\n"; MATCH: [1] php > echo "MATCH: [".fnmatch('/*test/*thing','/sometest/somethong')."]\n"; MATCH: [] php > echo "MATCH: [".fnmatch('/*test/','/sometest/somethong')."]\n"; MATCH: [] php > echo "MATCH: [".fnmatch('/*test/*','/sometest/somethong')."]\n"; MATCH: [1]
- «glob()» accepts a filespec that includes a file mask, and returns a list of matching filespecs.
- A file mask in this context can be e.g. «*.php» or «/home/woozle/*.php» but not «~/*.php».
- The «*» and «?» wildcards are both recognized.
- The mask may include multiple wildcards, for folders as well as files.
- Example: «/b*/b*» will return all files beginning with b inside root-level folders beginning with b.