-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
  • 24,679
  • 11
  • 53
  • 67

4 Answers4

1

You could use the below lookahead based regex.

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

DEMO

Avinash Raj
  • 166,785
  • 24
  • 204
  • 249
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
  • 130
  • 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

Drazzah
  • 7,371
  • 7
  • 31
  • 57