用模式匹配檔名

fnmatch

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

fnmatch用模式匹配檔名

說明

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

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 平臺上生效。

範例

示例 #1 用 shell 中的萬用字元模式匹配來檢查顏色名稱

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

註釋

警告

目前該函式無法在 Windows 或其它非 POSIX 相容的系統上使用。

參見

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *