Last active
January 29, 2016 08:16
-
-
Save m8rge/a58c2d41b2baad266940 to your computer and use it in GitHub Desktop.
Test string against simple mask with asterisk *
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 | |
class AsteriskMatch | |
{ | |
/** | |
* @param string $mask string with asterisk | |
* @param string $string tested subject | |
* @return bool | |
* @throws \Exception | |
*/ | |
public static function match($mask, $string) | |
{ | |
$regex = str_replace('\\*', '.*', preg_quote($mask, '/')); | |
$match = preg_match("/^$regex$/u", $string); | |
if (is_int($match)) { | |
return (bool)$match; | |
} else { | |
throw new \Exception('Preg error: ' . preg_last_error()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment