Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 667
Hefi Avatar asked Nov 20 '25 21:11

Hefi


1 Answers

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