Skip to main content
Topic: Getting last folder of a url (Read 1509 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Getting last folder of a url

I am modifying / extending a function of one of my addon and am trying to get the last folder in the url properly. I am actually trying to assign a membergroup based on the last folder name.

Currently I am testing the following code:
Code: [Select]
basename(dirname($url));

It works fine on several of my tests but the question is there any hidden "things" that I should know about the above code?

Re: Getting last folder of a url

Reply #1

I know it's not exactly the answer, though if I understood correctly what you are trying to achieve, I would not use the "position" inside a path to decide that, but I would use a kind of placeholder, for example:
Code: [Select]
$url = 'something/group/my_group';
$p = explode('/', $url);
$group = '';
$get_group = false;
foreach ($p => $d)
{
    if ($get_group === true)
    {
        $group = $d;
        break;
    }
    if ($d === 'group')
    {
        $get_group = true;
    }
}
or alternatively:
Code: [Select]
$url = 'something/group-my_group';
$p = explode('/', $url);
$group = '';

foreach ($p => $d)
{
    if (substr($d, 0, 5) === 'group')
    {
        $group = substr($d, 6);
    }
}

this would work somehow like an indexed array, so that you know what you are dealing with is actually what you are supposed to (and not maybe a wrong url).

Added missing code tag ... spuds
Last Edit: May 08, 2018, 08:03:00 am by Spuds
Bugs creator.
Features destroyer.
Template killer.