【问题标题】:Match URI by the asterisk用星号匹配 URI
【发布时间】:2014-02-18 11:25:23
【问题描述】:

我想做的是自动匹配 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] 以匹配描述)但我也想匹配星号...所以如果有像 controller/another/name 这样的控制器和我会使用像controller/another/* 这样的配置,它会匹配这个数组键的描述。

有人知道怎么做吗?我基本上需要想法,但欢迎任何其他答案!

【问题讨论】:

    标签: php regex arrays uri


    【解决方案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结果,这样就不需要一遍又一遍地调用它。

    【讨论】:

    • 太聪明了!不知道fnmatch 功能,太棒了。谢谢你:)
    【解决方案2】:

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

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

    controller/action/.*
    

    匹配

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

    【讨论】:

      【解决方案3】:

      建议的解决方案:

      这将返回与 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
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-27
        • 2020-03-19
        • 2015-05-26
        相关资源
        最近更新 更多