You don't need regular expression for that
There's more than one way of doing it.
Look into parse_url()
.
parse_url(): This function parses a URL and returns an associative array containing any of the various components of the URL that are present.
That will get you most of the way there and will also separate the host for you. Then you just explode your way to the last part using explode() and end().
$url = parse_url('http://example.com/project/controller/action/param1/param2');
$url['last'] = end(explode('/', $url[path]));
Array
(
[scheme] => http
[host] => example.com
[path] => /project/controller/action/param1/param2
[last] => param2
)
Or you can go straight to the point like this:
$last = ltrim(strrchr(parse_url($url, PHP_URL_PATH), '/'), '/');
You can also just go a head and use explode() in combination of end() directly on the URL. (it's also a lot shorter if you don't need the extra information of parse_url)
$last = end(explode('/', $url));
You can also just use basename() like this
$url = "http://example.com/project/controller/action/param1/param2";
$last = basename($url);
// Output: param2