コード例 #1
0
ファイル: util.php プロジェクト: yunsite/mapleleaf
function compare_like($value1, $value2)
{
    static $patterns = array();
    // Lookup precomputed pattern
    if (isset($patterns[$value2])) {
        $pat = $patterns[$value2];
    } else {
        // Calculate pattern
        $rc = 0;
        $mod = "";
        $prefix = "/^";
        $suffix = "\$/";
        // quote regular expression characters
        $str = preg_quote($value2, "/");
        // unquote \
        $str = str_replace("\\\\", "\\", $str);
        // Optimize leading/trailing wildcards
        if (substr($str, 0, 1) == '%') {
            $str = substr($str, 1);
            $prefix = "/";
        }
        if (substr($str, -1) == '%' && substr($str, -2, 1) != '\\') {
            $str = substr($str, 0, -1);
            $suffix = "/";
        }
        // case sensitive ?
        if (!LIKE_CASE_SENSITIVE) {
            $mod = "i";
        }
        // setup a StringParser and replace unescaped '%' with '.*'
        $sp = new StringParser();
        $sp->setConfig(array(), "\\", array());
        $sp->setString($str);
        $str = $sp->replaceCharWithStr("%", ".*");
        // replace unescaped '_' with '.'
        $sp->setString($str);
        $str = $sp->replaceCharWithStr("_", ".");
        $pat = $prefix . $str . $suffix . $mod;
        // Stash precomputed value
        $patterns[$value2] = $pat;
    }
    return preg_match($pat, $value1);
}