dongxieting9623 2009-08-18 15:40
浏览 72
已采纳

preg_match()(或其他php正则表达式函数)可以匹配可变数量的带括号的子模式吗?

Suppose I have '/srv/www/site.com/htdocs/system/application/views/' and want to test it against a regexp that matches each directory name in the path?

Something like this pattern: '(/[^/])'
That yields an array with 'srv','www','site.com'... etc.

PS: the regexp syntax I wrote is just to illustrate, it's not tested and surely wrong, but just to give an idea.

PS2: I know there's explode() but let's see if we can do this with a regexp (it's useful for other languages and frameworks which don't have explode).

  • 写回答

3条回答 默认 最新

  • douguizhuang8276 2009-08-18 15:51
    关注

    preg_match_all:

    $str = '/srv/www/site.com/htdocs/system/application/views/';
    
    preg_match_all('/\/([^\/]+)/', $str, $matches);
    
    // $matches[0] contains matching strings
    // $matches[1] contains first subgroup matches
    print_r($matches[1]);
    

    Output:

    Array
    (
        [0] => srv
        [1] => www
        [2] => site.com
        [3] => htdocs
        [4] => system
        [5] => application
        [6] => views
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题