Skip to content

Instantly share code, notes, and snippets.

@Inzman
Created January 8, 2020 07:58
Show Gist options
  • Save Inzman/b515afaff3dbe55d52d2ef5ba38fe99a to your computer and use it in GitHub Desktop.
Save Inzman/b515afaff3dbe55d52d2ef5ba38fe99a to your computer and use it in GitHub Desktop.
Get last segment path in URL
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