note 100207 added to function.fnmatch
| From: | me at rowanlewis dot com | Date: | Fri, 01 Oct 2010 05:58:43 +0000 |
| Subject: | note 100207 added to function.fnmatch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172862@lists.php.net to get a copy of this message | ||
The code below is not really useful, here is a better implementation with support for 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(
'\*' => '.*',
'\?' => '.',
'\[\!' => '[^',
'\[' => '[',
'\]' => ']',
'\.' => '\.',
'\\' => '\\\\'
);
// Forward slash in string must be in pattern:
if ($flags & FNM_PATHNAME) {
$transforms['\*'] = '[^/]*';
}
// Back slash should not be escaped:
if ($flags & FNM_NOESCAPE) {
unset($transforms['\\']);
}
// Perform case insensitive match:
if ($flags & FNM_CASEFOLD) {
$modifiers .= 'i';
}
// Period at start must be the same as pattern:
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.
----
Server IP: 117.104.162.229
Probable Submitter: 203.144.8.51 (proxied: 203.144.8.51)
----
Manual Page -- http://www.php.net/manual/en/function.fnmatch.php
Edit -- https://master.php.net/note/edit/100207
Del: integrated -- https://master.php.net/note/delete/100207/integrated
Del: useless -- https://master.php.net/note/delete/100207/useless
Del: bad code -- https://master.php.net/note/delete/100207/bad+code
Del: spam -- https://master.php.net/note/delete/100207/spam
Del: non-english -- https://master.php.net/note/delete/100207/non-english
Del: in docs -- https://master.php.net/note/delete/100207/in+docs
Del: other reasons-- https://master.php.net/note/delete/100207
Reject -- https://master.php.net/note/reject/100207
Search -- https://master.php.net/manage/user-notes.php