PHP fnmatch() Function

Definition and Usage

The fnmatch() function matches filenames or strings based on the specified pattern.

Syntax

fnmatch(pattern,string,flags)
Parameters Description
pattern Required. Specifies the pattern to be searched for.
string Required. Specifies the string or file to be checked.
flags Optional.

Description

This function is particularly useful for filenames but can also be used for ordinary strings. Ordinary users may be accustomed to shell mode or at least the simplest form of wildcard characters '?' and '*', so using fnmatch() instead of ereg() or preg_match() for front-end search expression input is more convenient for non-programmer users.

Tips and Comments

Important Note:Currently, this function cannot be used on Windows or other non-POSIX compatible systems.

Example

Check color names according to shell wildcard characters:

<?php
$txt = "My car is darkgrey..."
if (fnmatch("*gr[ae]y",$txt))
  {
  echo "some form of gray ...";