Search code examples
phpstringurlcharacterslash

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


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.


Solution

  • $last = explode("/", $str, 3);
    echo $last[2];