Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions
Keyboard Shortcuts
?
This help
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search
(current page)
Focus search box
中括号用于创建 ASCII 码点范围或字符集。例如,模式
"index.php[45]"
将匹配
"index.php4"
和
"index.php5"
,但不会匹配
"index.phpt"
。众所周知的范围是
[0-9]
、
[a-z]
和
[A-Z]
。可以同时使用多个集合和范围,例如
[0-9a-zABC]
。
me at rowanlewis dot com
¶
14 years ago
Here's a definitive solution, which supports negative character classes and the four documented flags.
<?php
if (!
function_exists
(
'fnmatch'
)) {
define
(
'FNM_PATHNAME'
,
1
);
define
(
'FNM_NOESCAPE'
,
2
);
define
(
'FNM_PERIOD'
,
4
);
define
(
'FNM_CASEFOLD'
,
16
);
function
fnmatch
(
$pattern
,
$string
,
$flags
=
0
) {
return
pcre_fnmatch
(
$pattern
,
$string
,
$flags
);
}
}
function
pcre_fnmatch
(
$pattern
,
$string
,
$flags
=
0
) {
$modifiers
=
null
;
$transforms
= array(
'\*'
=>
'.*'
,
'\?'
=>
'.'
,
'\[\!'
=>
'[^'
,
'\['
=>
'['
,
'\]'
=>
']'
,
'\.'
=>
'\.'
,
'\\'
=>
'\\\\'
);
if (
$flags
&
FNM_PATHNAME
) {
$transforms
[
'\*'
] =
'[^/]*'
;
}
if (
$flags
&
FNM_NOESCAPE
) {
unset(
$transforms
[
'\\'
]);
}
if (
$flags
&
FNM_CASEFOLD
) {
$modifiers
.=
'i'
;
}
if (
$flags
&
FNM_PERIOD
) {
if (
strpos
(
$string
,
'.'
) ===
0
&&
strpos
(
$pattern
,
'.'
) !==
0
) return
false
;
}
$pattern
=
'#^'
.
strtr
(
preg_quote
(
$pattern
,
'#'
),
$transforms
)
.
'$#'
.
$modifiers
;
return (boolean)
preg_match
(
$pattern
,
$string
);
}
?>
This probably needs further testing, but it seems to function identically to the native fnmatch implementation.
bernd dot ebert at gmx dot net
¶
13 years ago
There is a problem within the pcre_fnmatch-Function concerning backslashes. Those will be masked by preq_quote and ADDITONALLY by the strtr if FN_NOESCAPE is not set -> something like "*a(*" will finally result in "#^.*a\\(.*$#". Note the double backslash which effectively does NOT mask the "(" correctly.
Since preq_quote always matches a backslash I don't think that this'll work with using preg_quote at all.
theboydanny at gmail dot com
¶
17 years ago
About the windows compat functions below:
I needed fnmatch for a application that had to work on Windows, took a look here and tested both. Jk's works for me, soywiz didn't (on WinXPSP2, PHP 5.2.3).
The only difference between them is addcslashes (soywiz) instead of preg_quote (jk). They _should_ both work, but for some reason soywiz's didn't for me. So YMMV.
However, to make JK's fnmatch() work with the example in the documentation, you also have to strtr the [ and ] in $pattern.
<?php
$pattern
=
strtr
(
preg_quote
(
$pattern
,
'#'
), array(
'\*'
=>
'.*'
,
'\?'
=>
'.'
,
'\['
=>
'['
,
'\]'
=>
']'
));
?>
And thanks for the functions, guys.
phlipping at yahoo dot com
¶
22 years ago
you couls also try this function that I wrote before I found fnmatch:
function WildToReg($str)
{
$s = "";
for ($i = 0; $i < strlen($str); $i++)
{
$c = $str{$i};
if ($c =='?')
$s .= '.'; // any character
else if ($c == '*')
$s .= '.*'; // 0 or more any characters
else if ($c == '[' || $c == ']')
$s .= $c; // one of characters within []
else
$s .= '\\' . $c;
}
$s = '^' . $s . '$';
//trim redundant ^ or $
//eg ^.*\.txt$ matches exactly the same as \.txt$
if (substr($s,0,3) == "^.*")
$s = substr($s,3);
if (substr($s,-3,3) == ".*$")
$s = substr($s,0,-3);
return $s;
}
if (ereg(WildToReg("*.txt"), $fn))
print "$fn is a text file";
else
print "$fn is not a text file";