Please help me get everything after the last occurrence of /
using regex. My variables look like this:
在最后一次使用正则表达式后,请帮助我获取所有内容。我的变量看起来像这样:
http://domain.com/folder/subfolder/123456
http://domain.com/folder/subfolder/678901
etc.
and I only need the part after the last /
i.e.
我只需要在最后一个之后的部分
123456
678901
Currently I'm using /[^/]*$
but this gives me /123456
& /678901
.
目前我正在使用/ [^ /] * $但这给了我/ 123456和/ 678901。
2 个解决方案
#1
You should use brackets in regexp, than you can access by reference \1.
您应该在regexp中使用括号,而不是通过引用\ 1访问。
Example (if length not equals to 6 you should update it.):
示例(如果长度不等于6,则应更新它。):
<?php
preg_match('/\/(\d{6})$/', 'http://domain.com/folder/subfolder/123456', $m);
var_dump($m[1]);
Result:
string(6) "123456"
#2
#1
You should use brackets in regexp, than you can access by reference \1.
您应该在regexp中使用括号,而不是通过引用\ 1访问。
Example (if length not equals to 6 you should update it.):
示例(如果长度不等于6,则应更新它。):
<?php
preg_match('/\/(\d{6})$/', 'http://domain.com/folder/subfolder/123456', $m);
var_dump($m[1]);
Result:
string(6) "123456"