This code can compare a string to a wildcard, such as "a*c" which would match "abc" or "abbbbbc" or "afiefaififeanfnc" but not "aaa". It can also match "a?c" to "abc" or "a7c" but not "azzc".

Usage

$matches = wild_cmp("a string","*str*"); // $matches == true

Code

function wild_cmp($string, $wildcard) {
    return preg_match('/^' . str_replace(array('\\*','\\?'), array('(.*?)','(.)'), preg_quote($wildcard)) . '$/', $string);
}

Next Post Previous Post