4

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

Let's say my root directory is at /home/user/public_html/website/.

Here's how I echo the root directory:

echo $_SERVER['DOCUMENT_ROOT'];

it will display /home/user/public_html/website/

Here's how I echo a folder in the root directory:

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

it will display /home/user/public_html/website/about/

Question: How can I strip everything that it displays all the way up to the root folder and/or sub-folder.

Example: If I echo $_SERVER['DOCUMENT_ROOT'], I want it to display as / instead of the entire /home/user/public_html/website/ path.

and if I echo a sub-folder I want it to display as /about/ instead of /home/user/public_html/website/about/.

So, how can I get it to echo just the ending part of the $_SERVER['DOCUMENT_ROOT']; instead of the entire directory path?

I've tried dirname and basename but neither of those does what I need. I've thought of string replace but isn't there something much easier to add onto $_SERVER['DOCUMENT_ROOT'] that will just echo out the root folder or root folder plus sub-folder?

Update:

I ended up using the answer below from anant kumar singh but tweaking it using preg_split instead of explode because preg_split can take multiple string delimiters where as explode can take only 1.

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

If you know of a better way to accomplish what I'm trying to do, please post your answers below.

Gaurav Dave
  • 5,038
  • 9
  • 21
  • 37
Mike
  • 609
  • 5
  • 25
  • Have you tried something ? – Rizier123 May 05 '15 at 21:06
  • Ya I've tried lots of things so far, but can't find anything yet. dirname, basename. Is there something else out there besides these two? – Mike May 05 '15 at 21:07
  • ^ 1. Add your attempts/work into your question! 2. Take a look at `explode()` – Rizier123 May 05 '15 at 21:09
  • it's confusing. once you say ending part of document root and on the other side you say that for root folder you want to display /? can you please differentiate it? and you wan to just echo it or gona use for some other purpose? – Serving Quarantine period May 05 '15 at 21:11
  • as apposed to echoing the full path `/home/user/public_html/website/` where /website/ is the folder of the root, I instead want to echo out `/` which is the last trailing slash after the folder website. But if I'm echoing out a sub-directory like the about page `/home/user/public_html/website/about/`, I instead want it to display as `/about/`. – Mike May 05 '15 at 21:15
  • So why not simply use the single `/`? Basically what you're writing means `$_SERVER['DOCUMENT_ROOT']` should stand for a constant `/`?! Then simply use that. As in: `echo '/about/';` – Yoshi May 06 '15 at 06:44
  • it is not that simple. I have to use `$_SERVER['DOCUMENT_ROOT']`. I can't echo `/` because it has to come from `$_SERVER['DOCUMENT_ROOT']` not my own string. – Mike May 06 '15 at 06:46
  • well then `$x = substr($yourStringIncludingDocRoot, strlen($_SERVER['DOCUMENT_ROOT']) - 1);` But still I don't get it. The way you use it (even if forced) is fully redundant this way. – Yoshi May 06 '15 at 06:48
  • The reason I need to use it is because I'm using a navigation menu based on directory. The script is included on each page. It creates the menu nav with the tabs. Each tab represents each folder in the root directory. If I use `../` instead of `$_SERVER['DOCUMENT_ROOT']`, then the linking will not be correct on the menu if inside a sub-folder. If you want to see the full script let me know and I'll create a fiddle to show you. I'm sure it is not the best way to do it and perhaps you can critique it. – Mike May 06 '15 at 07:17
  • If you can provide a semi-runnable sample, I'll have a look at it. – Yoshi May 06 '15 at 07:59
  • please up-vote the answer also. thanks – Serving Quarantine period Feb 03 '16 at 09:46

1 Answers1

1

You can go with explode in the following manner:--

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

As you already said that preg_split is done the job for you:-

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

NOTE:- Don't be panic.I am not going to take your credit. I just did it for the future peoples purpose. If you put it as an answer then i will remove it.

Serving Quarantine period
  • 66,345
  • 10
  • 43
  • 85
  • I tweaked your answer a bit and used preg_split as I need to explode on more than one delimiter. – Mike May 05 '15 at 22:21
  • you can put your answer here and marked that. it will help others to find ther solution. – Serving Quarantine period May 05 '15 at 22:27
  • I tweaked your answer a bit because explode can not take more than 1 string delimiter but preg_split can take multiple. `$newArray = preg_split( " (-in/|-ca/|-co/) ", dirname($_SERVER['DOCUMENT_ROOT']) ); if(count($newArray)>1){ echo "/".$newArray[1]."/"; }else{ echo '/'; }` – Mike May 06 '15 at 01:21