Come ottenere l'ultimo percorso in un URL?
-
20-09-2019 - |
Domanda
Vorrei ottenere l'ultimo segmento di percorso in un URL:
-
http://blabla/bla/wce/news.php
o -
http://blabla/blablabla/dut2a/news.php
Per esempio, in questi due URL, voglio ottenere il segmento del percorso:. 'WCE', e 'dut2a'
Ho cercato di usare $_SERVER['REQUEST_URI']
, ma ho l'intero percorso URL.
Soluzione
Prova:
$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];
Supponendo $tokens
ha almeno 2 elementi.
Altri suggerimenti
Prova questo:
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']);
Ho anche provato con un paio di URL dai commenti. Ho intenzione di avere a supporre che tutti i percorsi terminano con una barra, perché non in grado di identificare se / bob è una directory o un file. Ciò assume è un file a meno che non dispone di una barra finale troppo.
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
è facile
<?php
echo basename(dirname($url)); // if your url/path includes a file
echo basename($url); // if your url/path does not include a file
?>
-
basename
restituirà il finale finale componente nome di percorso -
dirname
restituirà il percorso della directory principale
Prova questo:
$parts = explode('/', 'your_url_here');
$last = end($parts);
Un'altra soluzione:
$last_slash = strrpos('/', $url);
$last = substr($url, $last_slash);
1: ottenere la posizione ultima barra 2: ricevendo il sottostringa tra l'ultima barra e la fine della stringa
Guardate qui: TEST
Se si desidera elaborare un URL assoluto, quindi è possibile utilizzare parse_url()
(non funziona con relativa gli URL).
$url = 'http://aplicaciones.org/wp-content/uploads/2011/09/skypevideo-500x361.jpg?arg=value#anchor';
print_r(parse_url($url));
$url_path = parse_url($url, PHP_URL_PATH);
$parts = explode('/', $url_path);
$last = end($parts);
echo $last;
codice di esempio completo qui: http://codepad.org/klqk5o29
esplodere funzione
$arr = explode("/", $uri);
io ho scritto un po 'di funzione per ottenere l'ultimo dir / cartella di un URL. Funziona solo con / URL esistenti reali, non quelli teorici. Nel mio caso, che è sempre stato così, quindi ...
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
}
Lavora per me! Godere! E complimenti alle risposte precedenti !!!