fnmatch

Description

fnmatch() checks if the passed string would
match the given shell wildcard pattern.

Parameters

pattern

The shell wildcard pattern.

string

The tested string. This function is especially useful for filenames,
but may also be used on regular strings.

The average user may be used to shell patterns or at least in their
simplest form to '?' and '*'
wildcards so using fnmatch() instead of
preg_match() for
frontend search expression input may be way more convenient for
non-programming users.

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.

FNM_PERIOD:> Leading period in $string must be exactly matched by period in $pattern.

FNM_NOESCAPE:> Disable backslash escaping.

FNM_NOSYS:> Obsolescent.

FNM_FILE_NAME:> Alias of FNM_PATHNAME.

FNM_LEADING_DIR:> From fnmatch.h: /* Ignore `/...' after a match. */

FNM_CASEFOLD:> Caseless match.

Since they’re appearing in file.c, but are not available in PHP, we’ll have to define them ourselves:<?phpdefine('FNM_PATHNAME', 1);define('FNM_PERIOD', 4);define('FNM_NOESCAPE', 2);// GNU extensionsdefine('FNM_FILE_NAME', FNM_PATHNAME);define('FNM_LEADING_DIR', 8);define('FNM_CASEFOLD', 16);?>

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.