解析字符串 - 使用正则表达式或类似的东西?

发布于 2024-12-22 08:21:56 字数 506 浏览 1 评论 0原文

我正在编写路由类并且需要帮助。我需要解析 $controller 变量并将该字符串的一部分分配给另一个变量。这是 $controller 的示例:

$controller = "admin/package/AdminClass::display"
//$path = "admin/package";
//$class = "AdminClass";
//$method = "display";

$controller = "AdminClass::display";
//$path = "";
//$class = "AdminClass";
//$method = "display";

$controller = "display"
//$path = "";
//$class = "";
//$method = "display";

这三种情况就是我所需要的。是的,我可以编写很长的程序来处理这种情况,但我需要的是使用正则表达式和函数 preg_match_all 的简单解决方案

有什么建议吗?

I'm writing routing class and need help. I need to parse $controller variable and assign parts of that string to another variables. Here is examples of $controller:

$controller = "admin/package/AdminClass::display"
//$path = "admin/package";
//$class = "AdminClass";
//$method = "display";

$controller = "AdminClass::display";
//$path = "";
//$class = "AdminClass";
//$method = "display";

$controller = "display"
//$path = "";
//$class = "";
//$method = "display";

This three situations is all i need. Yes, i can write long procedure to handle this situations, but what i need is simple solution with regex, with function preg_match_all

Any suggestion how to do this?

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

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

发布评论

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

评论(3

追我者格杀勿论 2024-12-29 08:21:56

以下正则表达式应该为您完成此操作,然后您可以将捕获的组保存到 $path$class$method

(?:(.+)/)?(?:(.+)::)?(.+)

这是红宝石:
http://www.rubular.com/r/1vPIhwPUub

你的 php 代码可能看起来像这样:

$regex = '/(?:(.+)\/)?(?:(.+)::)?(.+)/';
preg_match($regex, $controller, $matches);
$path = $matches[1];
$class = $matches[2];
$method = $matches[3];

The following regex should accomplish this for you, you can then save the captured groups to $path, $class, and $method.

(?:(.+)/)?(?:(.+)::)?(.+)

Here is a Rubular:
http://www.rubular.com/r/1vPIhwPUub

Your php code might look something like this:

$regex = '/(?:(.+)\/)?(?:(.+)::)?(.+)/';
preg_match($regex, $controller, $matches);
$path = $matches[1];
$class = $matches[2];
$method = $matches[3];
热鲨 2024-12-29 08:21:56

这假设类内的路径和方法名称只能包含字母。

完整的正则表达式如下:

^(?:((?:[a-zA-Z]+/)*)([a-zA-Z]+)::)?([a-zA-Z]+)$

两个非捕获组:第一个使所有路径和类可选,第二个避免捕获单个路径元素。

说明:

  • 路径元素是一个或多个字母后跟一个/:[a-zA-Z]+/
  • 可能有零个或多个:我们必须将 * 量词应用于上面;但正则表达式不是原子,因此我们需要一个组。由于我们不想捕获单个路径元素,因此我们使用非捕获组:(?:[a-zA-Z]+/)*;
  • 我们想要捕获完整路径(如果存在),我们必须在此 ((?:[a-zA-Z]+/)*); 上使用捕获组;
  • 方法名称是一个或多个字母,我们要捕获它:([a-zA-Z]+);
  • 如果存在,则它遵循路径,并后跟两个分号: ((?:[a-zA-Z]+/)*)([a-zA-Z]+):: ;
  • 但所有这些都是可选的:因此,我们必须将所有这些都放在一个组中,而我们又不想捕获它们: (?:((?:[a-zA-Z]+/)*)([a -zA-Z]+)::)?;
  • 最后,它后面跟着一个方法名称,这一次不是可选的,我们想要捕获它: (?:((?:[a-zA-Z]+/)*)([a- zA-Z]+)::)?([a-zA-Z]+);
  • 我们希望它匹配整行:我们需要在开头和结尾都锚定它,这给出了最终结果: ^(?:((?:[a-zA-Z]+/ )*)([a-zA-Z]+)::)?([a-zA-Z]+)$

唷。

This supposes that paths within the class, and the method name, can only contain letters.

The full regex is the following:

^(?:((?:[a-zA-Z]+/)*)([a-zA-Z]+)::)?([a-zA-Z]+)$

Two non capturing groups: the first one which makes all the path and class optional, the second which avoids the capture of individual path elements.

Explanation:

  • a path element is one or more letters followed by a /: [a-zA-Z]+/;
  • there may be zero or more of them: we must apply the * quantifier to the above; but the regex is not an atom, we therefore need a group. As we do not want to capture individual path elements, we use a non capturing group: (?:[a-zA-Z]+/)*;
  • we want to capture the full path if it is there, we must use a capturing group over this ((?:[a-zA-Z]+/)*);
  • the method name is one or more letters, and we want to capture it: ([a-zA-Z]+);
  • if present, it follows the path, and is followed by two semicolons: ((?:[a-zA-Z]+/)*)([a-zA-Z]+)::;
  • but all this is optional: we must therefore put a group around all this, which again we do not want to capture: (?:((?:[a-zA-Z]+/)*)([a-zA-Z]+)::)?;
  • finally, it is followed by a method name, which is NOT optional this time, and which we want to capture: (?:((?:[a-zA-Z]+/)*)([a-zA-Z]+)::)?([a-zA-Z]+);
  • and we want this to match the whole line: we need to anchor it both at the beginning and at the end, which gives the final result: ^(?:((?:[a-zA-Z]+/)*)([a-zA-Z]+)::)?([a-zA-Z]+)$

Phew.

风启觞 2024-12-29 08:21:56
        $pieces = explode('/',$controller); 
        $path = '';
        for($i = $i<$pieces.length-1; $i++)
        {
          if($i != 0)
             $path+='/';
          $path += $pieces[$i];
        }
        $p2 = explode(  '::',$pieces[$pieces.length-1]);
        $class = $p2[0];
        $method = $p2[1];
        $pieces = explode('/',$controller); 
        $path = '';
        for($i = $i<$pieces.length-1; $i++)
        {
          if($i != 0)
             $path+='/';
          $path += $pieces[$i];
        }
        $p2 = explode(  '::',$pieces[$pieces.length-1]);
        $class = $p2[0];
        $method = $p2[1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文