简体   繁体   English

正则表达式匹配后的一切

[英]Regex match everything after

I have a url for which i want to match a certain pattern 我有一个网址,我想要匹配某种模式

/events/display/id/featured /事件/显示/ ID /特色

where 哪里

  • match everything after /events/ 在/ events /之后匹配一切
  • display is matched into key 显示匹配到密钥
  • id and featured are 1 or more matched into a key id和features是一个或多个匹配到一个键

thus i end up with 因此我最终得到了

Array (
 [method] => display
[param] => Array ([0]=>id,[1]=>featured,[2]=>true /* if there was another path */)
)

so far i have 到目前为止我有

(?:/events/)/(?P<method>.*?)/(?P<parameter>.*?)([^/].*?)

But its not working out as expected. 但它没有达到预期的效果。

What's wrong with the syntax? 语法有什么问题?

PS no i don't want to use parse_url() or php defined function i need a regex PS不,我不想使用parse_url()或php定义的函数我需要一个正则表达式

Why not using a mix of preg_match() and explode() ?: 为什么不混合使用preg_match()explode()

$str = '/events/display/id/featured';
$pattern = '~/events/(?P<method>.*?)/(?P<parameter>.*)~';
preg_match($pattern, $str, $matches);

// explode the params by '/'
$matches['parameter'] = explode('/', $matches['parameter']);
var_dump($matches);

Output: 输出:

array(5) {
  [0] =>
  string(27) "/events/display/id/featured"
  'method' =>
  string(7) "display"
  [1] =>
  string(7) "display"
  'parameter' =>
  array(2) {
    [0] =>
    string(2) "id"
    [1] =>
    string(8) "featured"
  }
  [2] =>
  string(11) "id/featured"
}

You can use this pattern: 您可以使用此模式:

<pre><?php
$subject = '/events/display/id1/param1/id2/param2/id3/param3';

$pattern = '~/events/(?<method>[^/]++)|\G/(?<id>[^/]++)/(?<param>[^/]++)~';

preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);

foreach($matches as $match) {
    if (empty($match['method'])) {
        $keyval[] = array('id'=>$match['id'], 'param'=>$match['param']);
    } else {
        $result['method'] = $match['method'];
    }
}
if (isset($keyval)) $result['param'] = $keyval;
print_r($result);

pattern details: 图案细节:

~
/events/(?<method>[^/]++)  # "events" followed by the method name 
|                          # OR
\G                         # a contiguous match from the precedent
/(?<id>[^/]++)             # id
/(?<param>[^/]++)          # param
~

Here I'm basically using preg_match_all() to recreate functionality similar to explode() . 在这里,我基本上使用preg_match_all()来重新创建类似于explode() Then I'm remapping the results to a new array. 然后我将结果重新映射到一个新数组。 Unfortunately this can't be done with Regex alone. 不幸的是,单靠Regex无法做到这一点。

<?php

$url = '/events/display/id/featured/something-else';
if(preg_match('!^/events!',$url)){
    $pattern = '!(?<=/)[^/]+!';
    $m = preg_match_all($pattern,$url,$matches);

    $results = array();

    foreach($matches[0] as $key=>$value){
        if($key==1){
            $results['method']=$value;  
        }   elseif(!empty($key)) {
            $results['param'][]=$value;
        }
    }
}

print_r($results);

?>

Output 产量

Array
(
    [method] => display
    [param] => Array
        (
            [0] => id
            [1] => featured
            [2] => something-else
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM