Question

Je voudrais obtenir le dernier segment de chemin dans une URL:

  • http://blabla/bla/wce/news.php ou
  • http://blabla/blablabla/dut2a/news.php

Par exemple, dans ces deux URL, je veux obtenir le segment de chemin. « WCE » et « dut2a »

J'ai essayé d'utiliser $_SERVER['REQUEST_URI'], mais je reçois tout le chemin d'URL.

Était-ce utile?

La solution

Essayez:

$url = 'http://blabla/blablabla/dut2a/news.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];

En supposant $tokens a au moins 2 éléments.

Autres conseils

Essayez ceci:

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']);

Je l'ai aussi testé avec quelques URL des commentaires. Je vais devoir supposer que tous les chemins se terminent par une barre oblique, parce que je ne peux pas identifier si / bob est un répertoire ou un fichier. Cela suppose qu'il est un fichier à moins qu'il ait un slash aussi.

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

il est 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 retourne le composant de nom de fuite arrière du chemin
  • dirname retourne le chemin du répertoire parent

http://php.net/manual/en/function.dirname.php

http://php.net/manual/en/function.basename.php

Essayez ceci:

 $parts = explode('/', 'your_url_here');
 $last = end($parts);

Une autre solution:

$last_slash = strrpos('/', $url);
$last = substr($url, $last_slash);

1: obtenir la dernière position de barre oblique 2: obtenir la sous-chaîne entre la dernière barre oblique et la fin de la chaîne

Regardez ici: TEST

Si vous voulez traiter une URL absolue, vous pouvez utiliser parse_url() (il ne fonctionne pas avec le parent uRLs).

$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;

Exemple de code complet ici: http://codepad.org/klqk5o29

exploser fonction

$arr =  explode("/", $uri);

Je me suis écrit une petite fonction pour obtenir le dernier répertoire / dossier d'une URL. Il fonctionne uniquement avec urls réels / existants, et non pas théoriques. Dans mon cas, cela a toujours été le cas, alors ...

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
}

Travaux pour moi! Prendre plaisir! Et bravo aux réponses précédentes !!!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top