Last active
November 3, 2022 01:24
-
-
Save dgrebb/3357c19a72c5c9daf59c to your computer and use it in GitHub Desktop.
PHP - get the last URL segment
This file contains 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
<?php | |
// finds the last URL segment | |
$urlArray = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); | |
$segments = explode('/', $urlArray); | |
$numSegments = count($segments); | |
$currentSegment = $segments[$numSegments - 1]; | |
?> | |
<?php if ( $currentSegment == "index.php" ) { ?>active<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use
end($segments);
.https://www.php.net/manual/en/function.end.php