Extracting the last segment on an URI

前端 未结 4 520
夕颜
夕颜 2020-12-12 03:48

String # 1:

/string/morestring/thename

String # 2:

/string/morestring/thename/

Regex:

[^\         


        
相关标签:
4条回答
  • 2020-12-12 04:00

    Another option:

    $last = array_pop(preg_split('#/+#', rtrim($s, '/')));
    
    0 讨论(0)
  • 2020-12-12 04:05

    jeroen's basename solution is very good but might not work on windows, it would also cut out the extension if the URI ends with .something too.

    I'd do this:

     $last = array_pop(explode('/',rtrim($s,'/')));
    
    0 讨论(0)
  • 2020-12-12 04:11

    I would just use basename().

    0 讨论(0)
  • 2020-12-12 04:18
    [^\/]+\/?$
    

    http://rubular.com/r/TeAEWM0jsd

    0 讨论(0)
提交回复
热议问题