-4

I have some URL like this:

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

How to get the last part which removed the last word?

hello-everybody
I-dont-care
I-love-you
user3942918
  • 25,539
  • 11
  • 55
  • 67

4 Answers4

1

You could use the below lookahead based regex.

[^\/]+?(?=-[^-\/]+$)

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

hope it can help you!

$str = 'http://www.mywebsite.com/at/hello-everybody-mn0000000003';
$str = preg_replace('@http://www.mywebsite.com/(.*)/@','',$str);
$str = substr($str,0,strrpos($str,'-'));
TuanTai
  • 140
  • 7
0

Unfortunately, this is the furthest I can get you:

(?!\/)(?:.(?!\/))+(?=-)

This is what it would match:

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
0

Try this RegEx: /[\w-]+(?=-)/.

Example: https://regex101.com/r/gX3sD2/1

Ian Hazzard
  • 7,661
  • 7
  • 34
  • 60