Rickstar
Rickstar

Reputation: 6189

How to remove the rest of a folder path in PHP

i have displayed the follow folders from my ftp it is taking it out correctly but i am trying to display the last folder is there anyway of doing this e.g str_replace but the is not exactly what i am looking for.

    httpdocs/user_images/ 
e.g user_images

    httpdocs/user_images/home 
e.g home

    httpdocs/user_images/home/lastest 
e.g lastest

    httpdocs/user_images/home/lastest/monday 
e.g monday

    httpdocs/user_images/home/lastest/friday 
e.g friday

Upvotes: 0

Views: 363

Answers (2)

Jeff Warnica
Jeff Warnica

Reputation: 772

use preg_split, and array_pop. Sample code:

<?php

$dirs = array("httpdocs/user_images/", "httpdocs/user_images/home", "httpdocs/user_images/home/lastest", "httpdocs/user_images/home/lastest/monday","httpdocs/user_images/home/lastest/friday");    
foreach ($dirs as $_) {  
                $bits = preg_split("|/|", $_);
                print $_ . " --> " . array_pop($bits) . "\n";
}

Upvotes: 0

NA.
NA.

Reputation: 6579

basename() is what you want.

Upvotes: 4

Related Questions