我有一些像这样的网址:
http://www.mywebsite.com/at/hello-everybody-mn0000000003
http://www.mywebsite.com/am/I-dont-care-mw0000322930
http://www.mywebsite.com/ccc/mood/I-love-you-d5207
如何获取删除最后一个单词的最后一部分?
hello-everybody
I-dont-care
I-love-you
答案 0 :(得分:1)
答案 1 :(得分:0)
$str = 'http://www.mywebsite.com/at/hello-everybody-mn0000000003';
$str = preg_replace('@http://www.mywebsite.com/(.*)/@','',$str);
$str = substr($str,0,strrpos($str,'-'));
答案 2 :(得分:0)
不幸的是,这是我能找到的最远的地方:
(?!\/)(?:.(?!\/))+(?=-)
这就是它的匹配:
http://www.mywebsite.com/at/hello-everybody-mn0000000003 Matches: hello-everybody
http://www.mywebsite.com/am/I-dont-care-mw0000322930 Matches: I-dont-care
http://www.mywebsite.com/ccc/mood/I-love-you-d5207 Matches: I-love-you
答案 3 :(得分:0)
试试此RegEx:/[\w-]+(?=-)/
。