Skip to content

Instantly share code, notes, and snippets.

@m8rge
Last active January 29, 2016 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m8rge/a58c2d41b2baad266940 to your computer and use it in GitHub Desktop.
Save m8rge/a58c2d41b2baad266940 to your computer and use it in GitHub Desktop.
Test string against simple mask with asterisk *
<?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