DiegoP. DiegoP. - 2 years ago 150
PHP Question

Get last word from URL after a slash in PHP

I need to get the very last word from an URL. So for example I have the following URL:


I need to get with PHP only "test", nothing else. I tried to use something like this:

$words = explode(' ', $_SERVER['REQUEST_URI']);
$showword = trim($words[count($words) - 1], '/');
echo $showword;

It does not work for me. Can you help me please?

Thank you so much!!

Answer Source

by using regex:

preg_match("/[^\/]+$/", "http://www.mydomainname.com/m/groups/view/test", $matches);
$last_word = $matches[0]; // test
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download