Created
November 8, 2015 15:01
-
-
Save fquffio/1c36623acf19201dac07 to your computer and use it in GitHub Desktop.
GLOB-style match
This file contains 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
<?php | |
if (!function_exists('glob_match')) { | |
/** | |
* Filter keys with a GLOB-style pattern. | |
* | |
* @param string $subject Test string. | |
* @param string $pattern GLOB-style pattern. | |
* @param int $flags Optional flags (only `GLOB_BRACE` is valid). | |
* @return bool Match. | |
*/ | |
function glob_match($subject, $pattern, $flags = 0) | |
{ | |
$pattern = preg_quote($pattern, '/'); | |
/** Replace `*` and `?`. */ | |
$pattern = preg_replace_callback('/\\\\([?*])/', function ($match) { | |
return '.' . (($match[1] == '*') ? '*' : ''); | |
}, $pattern); | |
/** Expand curly braces. */ | |
if ($flags & GLOB_BRACE == GLOB_BRACE) { | |
$pattern = preg_replace_callback('/\\\{(.*?)\\\}/', function ($match) { | |
return '(' . str_replace(',', '|', $match[1]) . ')'; | |
}, $pattern); | |
} | |
return preg_match('/^' . $pattern . '$/i', $subject); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment