PHP | fnmatch( ) Function

The fnmatch() function in PHP used to match a filename or string against a specified pattern. The pattern and the filename to be checked are sent as parameters to the fnmatch() function and it returns True if a match is found and False on failure.
fnmatch() function is now available for Windows platforms on the PHP 5.3.0 version.

Syntax:

fnmatch(pattern, string, flags)

Parameters Used:
The fnmatch() function in PHP accepts three parameter.



  1. pattern : It is a mandatory parameter which specifies the pattern to search for.
  2. string : It is a mandatory parameter which specifies the string or file to be checked.
  3. flags : It is an optional paaremeter which is used to specify flags or a combination of flags.
    The flags can be a combination of the following flags:

    • FNM_PATHNAME : It is used to specify slash in string only matches slash in the given pattern.
    • FNM_NOESCAPE : It is used to disable backslash escaping.
    • FNM_CASEFOLD : It is used for a caseless match.
    • FNM_PERIOD : It is used to specify a leading period in string must be exactly matched by period in the given pattern.

Return Value:
It returns True if a match is found and a False on failure.

Errors And Exceptions:

  1. The buffer must be cleared if the fnmatch() function is used multiple times.
  2. The fnmatch() function returns Boolean False but many times it happens that it returns a non-Boolean value which evaluates to False.

Below programs illustrate the fnmatch() function.

Program 1 Suppose there is a file named “gfg.txt”

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$check = "gfg.txt";
  
// fnmatch function used to check
 for file starting with g
if (fnmatch("*[g]*",$check))
{
   echo "gfg";
}
else
{
   echo "match not found";
}
?>

chevron_right


Output:

gfg

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$check = "GeeksforGeeks";
  
// fnmatch function used to check for 
// a word practice or practise
if (fnmatch("*Geeks[gfgj]orGeeks", $check))
    echo "Yes";
else
    echo "No";
?>

chevron_right


Output:

Yes

Program 3

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$check = 'GFG A computer science portal';
  
// fnmatch function used to check 
// for a word php without considering its case 
if (fnmatch("*[PUTgfg]*", $check, FNM_CASEFOLD))
    echo "Yes";
else
    echo "No";
  
  
?>

chevron_right


Output:

Yes

Program 4

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$check = "There is a back slash \ in this sentence";
  
// fnmatch function used to check for a \ 
if (fnmatch("*[\]*", $check, FNM_NOESCAPE))
    echo "back slash  (\)  in the sentence ";
 else
    echo "match not found";
?>

chevron_right


Output:

back slash  (\)  in the sentence

Reference:
http://php.net/manual/en/function.fnmatch.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.