Prueba esto:
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
}
echo getLastPathSegment($_SERVER['REQUEST_URI']);
también he probado con un par de direcciones URL de los comentarios. Voy a tener que asumir que todas las rutas terminan con una barra inclinada, porque no puedo identificar si/bob es un directorio o un archivo. Esto supondrá que es un archivo a menos que también tenga una barra inclinada.
echo getLastPathSegment('http://server.com/bla/wce/news.php'); // wce
echo getLastPathSegment('http://server.com/bla/wce/'); // wce
echo getLastPathSegment('http://server.com/bla/wce'); // bla
bahamut100, si esta es la respuesta correcta, entonces debe aceptarla haciendo clic en la marca de verificación blanca al lado. –
@ bahamut100, de nada. Aunque iría por (y acepto) la respuesta de Alex. –
@Bart K. ¡Está bien, el suyo también hará el trabajo! +1 – alex