Created
January 8, 2020 07:58
-
-
Save Inzman/b515afaff3dbe55d52d2ef5ba38fe99a to your computer and use it in GitHub Desktop.
Get last segment path in URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getLastPathSegment($url) { | |
$path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL | |
$pathTrimmed = trim($path, '/'); // normalise with no leading or trailing slash | |
$pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash | |
if (substr($path, -1) !== '/') { | |
array_pop($pathTokens); | |
} | |
return end($pathTokens); // get the last segment | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment