Created
April 11, 2015 23:44
-
-
Save Geolim4/ee90091c8f33e9270bae to your computer and use it in GitHub Desktop.
Simple wildcard pattern matching function, using built-in preg_match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This file is part of one of my future projects.