How to get string after second slash in url - using php?

Hefi picture Hefi · Aug 28, 2014 · Viewed 12.9k times · Source

How to get string after second slash in url? URL is different every time (more slashes), but every time I need the whole text after the second slash. How to do it?

I am using this code:

<?php
    $str = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $last = substr($str, strrpos($str, '/') - 1);
    echo $last;
?>

...but it works online with some characters after slash.

Thank you very much for help.

Answer

user2846400 picture user2846400 · Aug 28, 2014
$last = explode("/", $str, 3);
echo $last[2];