Echo end or last folder $ _SERVER ['DOCUMENT_ROOT'] in PHP

I need to use $_SERVER['DOCUMENT_ROOT'];scandir to display files and folders for my navigation menu.

Say my root directory is at /home/user/public_html/website/.

This is how I repeat the root directory:

echo $_SERVER['DOCUMENT_ROOT'];

will be displayed /home/user/public_html/website/

This is how I repeat the folder in the root directory:

echo $_SERVER['DOCUMENT_ROOT'].'about/';

will be displayed /home/user/public_html/website/about/

Question: How can I separate everything that it displays, down to the root folder and / or subfolder.

An example . If I am echo $_SERVER['DOCUMENT_ROOT'], I want it to appear as /instead of the whole path /home/user/public_html/website/.

and if I draw a subfolder, I want it to appear as /about/instead /home/user/public_html/website/about/.

, $_SERVER['DOCUMENT_ROOT']; ?

dirname basename, , . , - $_SERVER['DOCUMENT_ROOT'], ?

anant kumar singh, , preg_split , , preg_split , , 1.

$newArray = preg_split( " (-in/|-ca/|-co/) ", dirname($_SERVER['DOCUMENT_ROOT']) );
if(count($newArray)>1){
    echo '/'.$newArray[1].'/';
    }else{
        echo '/';
    }

, , .

+4
1

: -

$newArray = explode('website/',$_SERVER['DOCUMENT_ROOT']);
 if(count($newArray)>1){
   echo "/".$newArray[1];
}else{
    echo '/';
}

, preg_split : -

$newArray = preg_split( " (-in/|-ca/|-co/) ", dirname($_SERVER['DOCUMENT_ROOT']) ); if(count($newArray)>1){ echo "/".$newArray[1]."/"; }else{ echo '/'; }

. - . . . , .

+1

All Articles