fnmatch

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

fnmatch用模式匹配文件名

说明

fnmatch(string $pattern, string $filename, int $flags = 0): bool

fnmatch() 检查传入的 filename 是否匹配给出的 shell 统配符 pattern

参数

pattern

shell 统配符。

filename

要检查的字符串。 此函数对于文件名尤其有用,但也可以用于普通的字符串。

普通用户可能习惯于 shell 模式或者至少其中最简单的形式 '?''*' 通配符,因此使用 fnmatch() 来代替 preg_match() 来进行前端搜索表达式输入对于非程序员用户更加方便。

flags

flags 值可以是下列 flag 的任意组合,并用位运算符 OR (|) 连接。

fnmatch() flag 列表
Flag 说明
FNM_NOESCAPE 禁用反斜线转义。
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.

返回值

匹配则返回 true,否则返回 false

范例

示例 #1 用 shell 中的通配符模式匹配来检查颜色名称

<?php
if (fnmatch("*gr[ae]y", $color)) {
echo
"some form of gray ...";
}
?>

注释

警告

目前该函数无法在 Windows 或其它非 POSIX 兼容的系统上使用。

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top