数组搜索中的通配符

发布于 2024-12-10 17:20:33 字数 188 浏览 0 评论 0原文

是否可以在 array_search 中使用通配符?我想搜索字符串的一部分,然后搜索

类似的内容(带星号)

print $pos = array_search('abitofastring%', $vars['myarray']);
unset($vars['myarray'][$pos]);

Is is possible to use wildcards with array_search? I want to search for part of a string and then

something like(with an asterisk)

print $pos = array_search('abitofastring%', $vars['myarray']);
unset($vars['myarray'][$pos]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-12-17 17:20:33

是否可以在 array_search 中使用通配符?

否,但您可以使用正则表达式(支持通配符)和 preg_grep 函数。

示例:

$array = explode(',', "House,Car,Boat,Horse,Pool Boy");

# remove all strings from array that do not contain "ho"

$array = preg_grep('~ho~i', $array, PREG_GREP_INVERT);

则数组为:

Array
(
    [1] => Car
    [2] => Boat
    [4] => Pool Boy
)

由于编写正则表达式模式可能很复杂,因此使用一个辅助函数将 SQL LIKE 模式转换为正则表达式可能会很方便,这样可以更轻松地使用:

$array = explode(',', "House,Car,Boat,Horse,Pool Boy");

# Search for "Ho" at the beginning of each string

$regex = like_to_regex('Ho%');

$array = preg_grep($regex, $array, PREG_GREP_INVERT);

print_r($array);

/**
 * convert a MySQL LIKE pattern into a pcre pattern
 */
function like_to_regex($like, $casesensitive = FALSE, $escapechar = '\\')
{
    $pattern = sprintf('~(?<!%1$s)(%1$s{2}|%%|_)~', preg_quote($escapechar));
    $tokens = preg_split($pattern, $like, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    foreach($tokens as &$token)
    {
        switch($token)
        {
            case $escapechar.$escapechar:
                $token = preg_quote($escapechar);
                break;
            case '_':
                $token = '.';
                break;
            case '%':
                $token = '.*';
                break;
            default:
                $token = preg_quote($token);

        }
    }
    return sprintf('~^%s$~%s', implode('', $tokens), $casesensitive ? '':'i');
}

Is is possible to use wildcards with array_search?

No, but you can use regular expressions (which supports wildcards) and the preg_grep function.

Example:

$array = explode(',', "House,Car,Boat,Horse,Pool Boy");

# remove all strings from array that do not contain "ho"

$array = preg_grep('~ho~i', $array, PREG_GREP_INVERT);

The array then is:

Array
(
    [1] => Car
    [2] => Boat
    [4] => Pool Boy
)

As it can be complicated to write regular expression patterns, it might be handy to have a helper function that converts your SQL LIKE pattern into a regular expression so this can be more easily used:

$array = explode(',', "House,Car,Boat,Horse,Pool Boy");

# Search for "Ho" at the beginning of each string

$regex = like_to_regex('Ho%');

$array = preg_grep($regex, $array, PREG_GREP_INVERT);

print_r($array);

/**
 * convert a MySQL LIKE pattern into a pcre pattern
 */
function like_to_regex($like, $casesensitive = FALSE, $escapechar = '\\')
{
    $pattern = sprintf('~(?<!%1$s)(%1$s{2}|%%|_)~', preg_quote($escapechar));
    $tokens = preg_split($pattern, $like, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    foreach($tokens as &$token)
    {
        switch($token)
        {
            case $escapechar.$escapechar:
                $token = preg_quote($escapechar);
                break;
            case '_':
                $token = '.';
                break;
            case '%':
                $token = '.*';
                break;
            default:
                $token = preg_quote($token);

        }
    }
    return sprintf('~^%s$~%s', implode('', $tokens), $casesensitive ? '':'i');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文