Laravel 4 输入::仅通配符

问题描述 投票:0回答:2

有没有办法在 Laravel 4 中的 Input::only 中使用通配符?

例如:

$actInputs = Input::only('act*');

只给我以字符串

act
开头的输入。

php laravel-4
2个回答
1
投票

这有效:

$actInputs = array();
foreach (Input::all() as $id => $value) {
   if (preg_match('/^act(\w+)/i', $id))
      $actInputs[$id] = $value;
}

0
投票

我想到了另一种方法

(inputStartsWith、inputEndsWith 和 InputMatching)

// inputStartsWith a string
function inputStartsWith($pattern = null)
{
    $input = Input::all(); $result = array();
    array_walk($input, function ($v, $k) use ($pattern, &$result) {
        if(starts_with($k, $pattern)) {
            $result[$k] = $v;
        }
    });
    return $result;
}

像这样使用它:

$inputs = inputStartsWith('act');

更新:(也

inputEndsWith

// inputEndsWith a string
function inputEndsWith($pattern = null)
{
    $input = Input::all(); $result = array();
    array_walk($input, function ($v, $k) use ($pattern, &$result) {
        if(ends_with($k, $pattern)) {
            $result[$k] = $v;
        }
    });
    return $result;
}

像这样使用它:

$inputs = inputEndsWith('_name');

可以将它们用作

helper
函数或
extend
core
类并添加这些函数。

更新:(模式匹配)

function InputMatching($pattern) {
    $input = Input::all();
    return array_intersect_key(
        $input,
        array_flip(preg_grep($pattern, array_keys($input), 0))
    );
}

像这样使用它:

// will match 'first_name1' and 'first_name2' (ends with digit)
$inputs = InputMatching("/^.*\d$/");

这可能会有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.