<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Match filename against a pattern</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.flock.html">flock</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fopen.html">fopen</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.fnmatch" class="refentry"> <div class="refnamediv"> <h1 class="refname">fnmatch</h1> <p class="verinfo">(PHP 4 >= 4.3.0, PHP 5)</p><p class="refpurpose"><span class="refname">fnmatch</span> — <span class="dc-title">Match filename against a pattern</span></p> </div> <div class="refsect1 description" id="refsect1-function.fnmatch-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>fnmatch</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>fnmatch()</strong></span> checks if the passed <em><code class="parameter">string</code></em> would match the given shell wildcard <em><code class="parameter">pattern</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.fnmatch-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">pattern</code></em></span> <dd> <p class="para"> The shell wildcard pattern. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">string</code></em></span> <dd> <p class="para"> The tested string. This function is especially useful for filenames, but may also be used on regular strings. </p> <p class="para"> The average user may be used to shell patterns or at least in their simplest form to <em>'?'</em> and <em>'*'</em> wildcards so using <span class="function"><strong>fnmatch()</strong></span> instead of <span class="function"><a href="function.preg-match.html" class="function">preg_match()</a></span> for frontend search expression input may be way more convenient for non-programming users. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">flags</code></em></span> <dd> <p class="para"> The value of <em><code class="parameter">flags</code></em> can be any combination of the following flags, joined with the <a href="language.operators.bitwise.html" class="link">binary OR (|) operator</a>. <table class="doctable table"> <caption><strong> A list of possible flags for <span class="function"><strong>fnmatch()</strong></span> </strong></caption> <thead> <tr> <th><em><code class="parameter">Flag</code></em></th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td><strong><code>FNM_NOESCAPE</code></strong></td> <td> Disable backslash escaping. </td> </tr> <tr> <td><strong><code>FNM_PATHNAME</code></strong></td> <td> Slash in string only matches slash in the given pattern. </td> </tr> <tr> <td><strong><code>FNM_PERIOD</code></strong></td> <td> Leading period in string must be exactly matched by period in the given pattern. </td> </tr> <tr> <td><strong><code>FNM_CASEFOLD</code></strong></td> <td> Caseless match. Part of the GNU extension. </td> </tr> </tbody> </table> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.fnmatch-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> if there is a match, <strong><code>FALSE</code></strong> otherwise. </p> </div> <div class="refsect1 changelog" id="refsect1-function.fnmatch-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.0</td> <td> This function is now available on Windows platforms. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.fnmatch-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2384"> <p><strong>Example #1 Checking a color name against a shell wildcard pattern</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">fnmatch</span><span style="color: #007700">(</span><span style="color: #DD0000">"*gr[ae]y"</span><span style="color: #007700">, </span><span style="color: #0000BB">$color</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"some form of gray ..."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.fnmatch-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> For now, this function is not available on non-POSIX compliant systems except Windows. </p> </div> </div> <div class="refsect1 seealso" id="refsect1-function.fnmatch-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.glob.html" class="function" rel="rdfs-seeAlso">glob()</a> - Find pathnames matching a pattern</span></li> <li class="member"> <span class="function"><a href="function.preg-match.html" class="function" rel="rdfs-seeAlso">preg_match()</a> - Perform a regular expression match</span></li> <li class="member"> <span class="function"><a href="function.sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a> - Parses input from a string according to a format</span></li> <li class="member"> <span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - Output a formatted string</span></li> <li class="member"> <span class="function"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.flock.html">flock</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fopen.html">fopen</a></div> <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>