★ wanayoo — archive 1999 http://www.php.net/manual/tr/function.fnmatch.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<flockfopen>
view the version of this page
Last updated: Thu, 21 Aug 2003

fnmatch

(PHP 4 >= 4.3.0)

fnmatch -- Match filename against a pattern

Description

array fnmatch ( string pattern, string string [, int flags])

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

This is especialy usefull 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 ereg() or preg_match() for frontend search expression input may be way more convenient for non-programming users.

Örnek 1. Checking a color name against a shell wildcard pattern.

<?php
if(fnmatch("*gr[ae]y", $color)) {
  echo "some form of gray ...";
}
?>

See also glob(), ereg(), preg_match() and the unix manpage on fnmatch(3) for flag names (as long as they are not documented here ).



add a note add a note User Contributed Notes
fnmatch
phlipping at yahoo dot com
06-Aug-2003 03:59
you couls also try this function that I wrote before I found fnmatch:

function WildToReg($str)
{
  $s = "";   
  for ($i = 0; $i < strlen($str); $i++)
  {
   $c = $str{$i};
   if ($c =='?')
    $s .= '.'; // any character
   else if ($c == '*')   
    $s .= '.*'; // 0 or more any characters   
   else if ($c == '[' || $c == ']')
    $s .= $c;  // one of characters within []
   else
    $s .= '\\' . $c;
  }
  $s = '^' . $s . '$';

  //trim redundant ^ or $
  //eg ^.*\.txt$ matches exactly the same as \.txt$
  if (substr($s,0,3) == "^.*")
   $s = substr($s,3);
  if (substr($s,-3,3) == ".*$")
   $s = substr($s,0,-3);
  return $s;
}

if (ereg(WildToReg("*.txt"), $fn))
  print "$fn is a text file";
else
  print "$fn is not a text file";
michael at zend dot com
18-Apr-2003 04:18
Try this one, supporting all filename matching facilities:

  // MY_FNMATCH
  function my_fnmatch($pattern, $file)
  {
    for($i=0; $i<strlen($pattern); $i++) {
      if($pattern[$i] == "*") {
        for($c=$i; $c<max(strlen($pattern), strlen($file)); $c++) {
          if(my_fnmatch(substr($pattern, $i+1), substr($file, $c))) {
            return true;
          }
        }
        return false;
      }
      if($pattern[$i] == "[") {
        $letter_set = array();
        for($c=$i+1; $c<strlen($pattern); $c++) {
          if($pattern[$c] != "]") {
            array_push($letter_set, $pattern[$c]);
          }
          else break;
        }
        foreach ($letter_set as $letter) {
          if(my_fnmatch($letter.substr($pattern, $c+1), substr($file, $i))) {
            return true;
          }
        }
        return false;
      }
      if($pattern[$i] == "?") {
        continue;
      }   
      if($pattern[$i] != $file[$i]) {
        return false;
      }
    }

    return true;
  }
me at lejfdiecks dot de
15-Apr-2003 07:46
I had the same problem ("Call to undefined function: fnmatch()") using PHP 4.2.3 on a hosted webserver; so I wrote "my own" fnmatch(). Hope it helps someone:

function fnmatch($strPattern, $strString)
// My fnmatch()
// Supports '?' and '*' as wildcards
{

  $intPos = 0;
  do
  {
    $strPatternChar = substr($strPattern, $intPos, 1);
    $strStringChar  = substr($strString, $intPos, 1);

    if ($strPatternChar == '*')
    {
      $bolMatch = TRUE;
      break;
    }
    $bolMatch = ($strPatternChar == $strStringChar) || ($strPatternChar == '?' && $strStringChar != '');
    $intPos++;
  } while (($bolMatch == TRUE) && ($intPos < max(strlen($strPattern), strlen($strString))));

  return($bolMatch);
}

// Main program - test fnmatch() function
if (fnmatch('te?t', 'test'))
  echo "Pattern <b>matches</b> string.";
else
  echo "Pattern <b>does not</b> match string.";
06-Mar-2003 12:29
Fatal error: Call to undefined function: fnmatch() in d:\www\dnw\generatr[17].php on line 262

seems fnmatch () isn't included in win32 version of php 4.3.0 :/

<flockfopen>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: Web Hosting Talk
Last updated: Fri Sep 26 23:11:24 2003 EDT