Link to home
Start Free TrialLog in
Avatar of SSupreme
SSupremeFlag for Belarus

asked on

URL directory echo

I need extract few directories from URL except first one if length = 2, for example:

experts-exchange.com/WD/Web_Languages-Standards/PHP/newQuestionWizard.jsp

I need to only bold part and ignore first directory as it's length equal 2;

experts-exchange.com/Web_Development/Web_Languages-Standards/PH/newQuestionWizard.jsp

I need all directories as first one isn't equal 2, and it doesn't matter what length of others.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This version accounts for the scheme also:

$result = preg_replace('#^(?:[^:]+://)?[^/]*|[^/]*$#', '', $input);   // Remove leading scheme and host
$result = preg_replace('#^/?[^/]{2}(?=/)#', '', $result);             // Remove first dir if it's 2 characters

Open in new window

Avatar of SSupreme

ASKER

Excuse me, which scheme?
Scheme as in "http:", "ftp:", "gopher:", etc. I didn't know if you had this (your example doesn't show it), but I included it just in case.
ok it works 100%, I guess it was easy for you, so could you please suggest how to show name of directory if it's first and length = 2.
You are Genius Indeed, thanks
so could you please suggest how to show name of directory if it's first and length = 2.
If I understand your question correctly, then I believe what you could do would be to change the second preg_replace to:

preg_match('#^(/?[^/]{2}(?=/))?(.*)$#', $result, $matches);             // Remove first dir if it's 2 characters

Open in new window


You would have the 2-character directory (or empty string) in the second index of $matches, and you would have the remaining directories in the third index. In other words:

var_dump($matches[1]);  // 2-character directory (or empty string)
var_dump($matches[2]);  // Remaining directories

Open in new window