Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Filesystem Functions : fnmatch

fnmatch

Match filename against a pattern (PHP 4 >= 4.3.0, PHP 5)
bool fnmatch ( string pattern, string string [, int flags] )

Example 639. Checking a color name against a shell wildcard pattern

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

Code Examples / Notes » fnmatch

phlipping

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";


jsnell

The last line of soywiz at gmail dot com windows replacement should be changed to:
  return preg_match('/' . $npattern . '$/i', $string);
otherwise, a pattern for *.xml will match file.xml~ or any else anything with the text *.xml in it, regardless of position.


frederik krautwald

soywiz's function still doesn't seem to work -- at least not with PHP 5.2.3 on Windows -- but jk's does.

jk

soywiz's function didnt seem to work for me, but this did.
<?php
if(!function_exists('fnmatch')) {
function fnmatch($pattern, $string) {
return preg_match("#^".strtr(preg_quote($pattern, '#'), array('\*' => '.*', '\?' => '.'))."$#i", $string);
} // end
} // end if
?>


sinured

Possible flags (scratched out of fnmatch.h):
...::...
FNM_PATHNAME:
> Slash in $string only matches slash in $pattern.
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:
<?php
define('FNM_PATHNAME', 1);
define('FNM_PERIOD', 4);
define('FNM_NOESCAPE', 2);
// GNU extensions
define('FNM_FILE_NAME', FNM_PATHNAME);
define('FNM_LEADING_DIR', 8);
define('FNM_CASEFOLD', 16);
?>
I didn’t test any of these except casefold, which worked for me.


soywiz

A revised better alternative for fnmatch on windows. It should work well on PHP >= 4.0.0
<?php
if (!function_exists('fnmatch')) {
function fnmatch($pattern, $string) {
return @preg_match(
'/^' . strtr(addcslashes($pattern, '/\\.+^$(){}=!<>|'),
array('*' => '.*', '?' => '.?')) . '$/i', $string
);
}
}
?>


Change Language


Follow Navioo On Twitter
basename
chgrp
chmod
chown
clearstatcache
copy
delete
dirname
disk_free_space
disk_total_space
diskfreespace
fclose
feof
fflush
fgetc
fgetcsv
fgets
fgetss
file_exists
file_get_contents
file_put_contents
file
fileatime
filectime
filegroup
fileinode
filemtime
fileowner
fileperms
filesize
filetype
flock
fnmatch
fopen
fpassthru
fputcsv
fputs
fread
fscanf
fseek
fstat
ftell
ftruncate
fwrite
glob
is_dir
is_executable
is_file
is_link
is_readable
is_uploaded_file
is_writable
is_writeable
lchgrp
lchown
link
linkinfo
lstat
mkdir
move_uploaded_file
parse_ini_file
pathinfo
pclose
popen
readfile
readlink
realpath
rename
rewind
rmdir
set_file_buffer
stat
symlink
tempnam
tmpfile
touch
umask
unlink
eXTReMe Tracker