0

I have a variable like

$dir = "dir1/dir2/dir3"etc..etc..

I want to extract dir1 from this string, which is the root directory name. How can I do this using PHP?

1

2 Answers 2

4

Try this:

$dirArr = explode('/', $dir);
$rootDir = $dirArr[0];
Sign up to request clarification or add additional context in comments.

Comments

1

The simplest answer would be to $dirs = explode('/', $dir) and then array_shift($dirs) but thats not as robust as it could be. Do you know where this is on your filesystem? A better answer will reveal itself with more context...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.