Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing a path 1

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
Hi All,
I need to parse out a path like:
/myfolder/folder2/file.php

I need to separate this out into the sub pieces and not quite sure how to approach it in PHP.
Is there a command similar to the Javascript split() so I can create an array splitting elements at the /?

I need to identify the first and second folder names to determine how the navigation control displays.
I cannot use parse_url as it does not work well with relative paths and because the value of the path is passed in via an htaccess file as a parameter on the querystring, not as the original URL/URI.

I know how I would do this in Javascript but do not know the commands available in PHP well enough yet to know what a comparable command might be.

Thanks.

At my age I still learn something new every day, but I forget two others.
 
Use the pathinfo function (Also see the explode function, into which you can pass the "/" character to split the folder names.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Thanks, I found the explode function and it seems to be working well.
All the functions with names similar to what I would use in Javascript or VBScript were in fact very different than I expected so it took a lot more reading until I found explode. :)



At my age I still learn something new every day, but I forget two others.
 
theniteowl,

You can combine [tt]explode()[/tt] with [tt]list()[/tt] like this:

Code:
<?php

$fn = "/myfolder/folder2/file.php";

list($blank, $folder, $sub_folder, $file_name) = explode("/", $fn);

echo "Folder : $folder<br />";
echo "Sub-folder : $sub_folder<br />";
echo "File name : $file_name<br />";

?>

This assumes, that you know the structure of the path in advance, ofcourse (like here: /folder/sub_folder/file_name)

Regards
 
Unfortunately I will not know the path in advance. Most times it will be subfolder/file but it could go more.

Just using explode though I can get the size of the array and loop through pulling out the values I need. The path will always be relative to the root which makes it easier.

Thanks.


At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top