Skip to content

Instantly share code, notes, and snippets.

@Geolim4
Created April 11, 2015 23:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Geolim4/ee90091c8f33e9270bae to your computer and use it in GitHub Desktop.
Save Geolim4/ee90091c8f33e9270bae to your computer and use it in GitHub Desktop.
Simple wildcard pattern matching function, using built-in preg_match
function match_wildcard( $wildcard_pattern, $haystack ) {
$regex = str_replace(
array("\*", "\?"), // wildcard chars
array('.*','.'), // regexp chars
preg_quote($wildcard_pattern)
);
return preg_match('/^'.$regex.'$/is', $haystack);
}
$test = "foobar and blob\netc.";
var_dump(
match_wildcard('foo*', $test), // TRUE
match_wildcard('bar*', $test), // FALSE
match_wildcard('*bar*', $test), // TRUE
match_wildcard('**blob**', $test), // TRUE
match_wildcard('*a?d*', $test), // TRUE
match_wildcard('*etc**', $test) // TRUE
);
@Geolim4
Copy link
Author

Geolim4 commented Apr 11, 2015

This file is part of one of my future projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment