Skip to content

Instantly share code, notes, and snippets.

@fquffio
Created November 8, 2015 15:01
Show Gist options
  • Save fquffio/1c36623acf19201dac07 to your computer and use it in GitHub Desktop.
Save fquffio/1c36623acf19201dac07 to your computer and use it in GitHub Desktop.
GLOB-style match
<?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