通过星号匹配URI

时间:2014-02-18 11:25:23

标签: php regex arrays uri

我想要做的是自动匹配URI,并为它们分配正确的描述。假设我有以下URI:

test
something/action
controller/test
controller/another
controller/action/something
action
action/test
controller/another/somethingelse

它们实际上是随机的,但现在我想使用以下数组(数组,因为我认为这将是最佳解决方案),以匹配它们:

$config = array(
     'test' => 'The description of test',
     'something/action' => 'Some action description',
     'controller/another' => 'Another description',
     'controller/action/*' => 'This should match the `controller/action/blah` and everything similar like: `controller/action/something` and give this description'
     'action' => 'Action description',
     'action/*' => 'As the 2nd above, this should match the `action/test` itself, and similar like `action/anothertest` URI
);

如果没有星号,这很容易......(只需$config[$uri]来匹配描述)但我也希望匹配星号...所以如果有一个像{{{}那样的控制器1}}我将使用类似controller/another/name的配置,它将匹配此数组键的描述。

任何人都知道如何做到这一点?我基本上需要创意,但欢迎任何其他答案!

3 个答案:

答案 0 :(得分:1)

你想要的是.*.位于正则表达式the character class, that matches any character中(默认情况下除了换行符),然后您需要quantifier *来表示匹配任何字符0次或更多次。

所以你的正则表达式看起来像

controller/action/.*

匹配

  

控制器/动作/
  控制器/动作/嗒嗒
  控制器/动作/东西
  ...

答案 1 :(得分:1)

这可以通过合并fnmatcharray_filter之类的内容来完成:

$str=file_get_contents(/*... URIs ...*/);
$config = array(
     'test' => 'The description of test',
     'something/action' => 'Some action description',
     'controller/another' => 'Another description',
     'controller/action/*' => 'This should match the `controller/action/blah` and everything similar like: `controller/action/something` and give this description',
     'action' => 'Action description',
     'action/*' => 'As the 2nd above, this should match the `action/test` itself, and similar like `action/anothertest` URI'
);
foreach(explode("\n",$str) as $line)
{
    $line=trim($line);
    $dkey=array_filter(array_keys($config),function($key)use($line){return fnmatch($key,$line);});
    if(count($dkey)<=0)
    {
        echo "(no description)";
    }
    else
    {
        echo $config[reset($dkey)];
    }
    echo "\n";
}

Online demo

以上代码输出:

The description of test
Some action description
(no description)
Another description
This should match the `controller/action/blah` and everything similar like: `controller/action/something` and give this description
Action description
As the 2nd above, this should match the `action/test` itself, and similar like `action/anothertest` URI
(no description)

可以改进上面的代码:您可以将array_keys结果缓存到外部循环中,这样就不需要反复调用它。

答案 2 :(得分:1)

建议的解决方案:

这将返回与uri相当的所有描述。这里有改进的余地。干杯!

<?php

    $uris = array(
        'test',
        'something/action',
        'controller/test',
        'controller/another',
        'controller/action/something',
        'action',
        'action/test',
        'controller/another/somethingelse'
    );

    $sUri = $uris[4];
    print_r(
        getConfigBasedOnKey($sUri)
    );

    function getConfigBasedOnKey($sUri) {

        $config = array(
             'test' => 'The description of test',
             'something/action' => 'Some action description',
             'controller/another' => 'Another description',
             'controller/action/*' => 'This should match the controller/action/blah and everything similar like: controller/action/something and give this description',
             'action' => 'Action description',
             'action/*' => 'As the 2nd above, this should match the `action/test` itself, and similar like action/anothertest URI'
        );

        $descriptions = array();
        foreach($config as $configKey => $configDescription) {
            $configKey = preg_replace("/\*/",".*",$configKey);

            preg_match("|^$configKey$|",$sUri,$matches);
            if(count($matches) > 0) {
                $descriptions[] = $configDescription;
            }
        }

        return $descriptions;
    }

?>

输出:

Array
(
    [0] => This should match the controller/action/blah and everything similar like: controller/action/something and give this description
)