Skip to content

Instantly share code, notes, and snippets.

@dgrebb
Last active November 3, 2022 01:24
Show Gist options
  • Save dgrebb/3357c19a72c5c9daf59c to your computer and use it in GitHub Desktop.
Save dgrebb/3357c19a72c5c9daf59c to your computer and use it in GitHub Desktop.
PHP - get the last URL segment
<?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 } ?>
@amiad
Copy link

amiad commented Nov 15, 2021

You can use end($segments);.
https://www.php.net/manual/en/function.end.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment