导航首页 » 技术教程 » PHP:fnmatch()的用法_Filesystem函数
PHP:fnmatch()的用法_Filesystem函数 159 2023-12-15   

fnmatch

(PHP 4 >= 4.3.0, PHP 5)

fnmatch — 用模式匹配文件名

说明

bool fnmatch ( string $pattern , string $string [, int $flags = 0 ] )

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

参数

pattern

shell 统配符。

string

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

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

flags

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.

返回值

匹配则返回 TRUE,否则返回 FALSE

更新日志

版本 说明 5.3.0 此函数开始在 Windows 平台上生效。

范例

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

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

注释

Warning

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

参见

glob() - 寻找与模式匹配的文件路径 preg_match() - 执行一个正则表达式匹配 sscanf() - 根据指定格式解析输入的字符 printf() - 输出格式化字符串 sprintf() - Return a formatted string


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103