Skip to content

Instantly share code, notes, and snippets.

@gollum23
Last active July 6, 2019 20:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gollum23/0b861de47ced51f8a50e to your computer and use it in GitHub Desktop.
Save gollum23/0b861de47ced51f8a50e to your computer and use it in GitHub Desktop.
Get last part URL with php
function uf_getLastDir($sUrl)
{
$sPath = parse_url($sUrl, PHP_URL_PATH); // parse URL and return only path component
$aPath = explode('/', trim($sPath, '/')); // remove surrounding "/" and return parts into array
end($aPath); // last element of array
if (is_dir($sPath)) // if path points to dir
return current($aPath); // return last element of array
if (is_file($sPath)) // if path points to file
return prev($aPath); // return second to last element of array
return false; // or return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment