Re: String Parse Help for novice

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Jun 13, 2010, at 5:40 PM, Ashley Sheridan wrote:

On Sun, 2010-06-13 at 17:35 -0500, Karl DeSaulniers wrote:

On Jun 13, 2010, at 5:31 PM, Ashley Sheridan wrote:

> On Sun, 2010-06-13 at 17:27 -0500, Karl DeSaulniers wrote:
>
>> On Jun 13, 2010, at 5:23 PM, Ashley Sheridan wrote:
>>
>>> On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote:
>>>
>>>> Hello List.
>>>>
>>>> I need to parse the PATH portion of URL. I have assigned the path
>>>> portion to a variable using the following:
>>>>
>>>> $thepath = parse_url($url);
>>>>
>>>>
>>>> Now I need to break each portion of the path down into its own
>>>> variable.  The problem is, the path can vary considerably as
>>>> follows:
>>>>
>>>> /mydirectory/mysubdirectory/anothersubdirectory/mypage.php
>>>>
>>>> vs.
>>>>
>>>> /mydirectory/mypage.php
>>>>
>>>> How do I get the either of the above url paths broken out so the
>>>> variables equal the following
>>>>
>>>> $dir1 = mydirectory
>>>> $dir2 = mysubdirectory
>>>> $dir3 = anothersubdirectory
>>>> $page = mypage.php
>>>>
>>>> ...etc... if there were 5 more subdirectories... they would be
>>>> dynamically assigned to a variable.
>>>>
>>>>   Thanks for any help.
>>>>
>>>>   --Rick
>>>>
>>>>
>>>>
>>>
>>>
>>> $filename = basename($path);
>>> $parts = explode('/', $path);
>>> $directories = array_pop($parts);
>>>
>>> Now you have your directories in the $directories array and the
>>> filename
>>> in $filename.
>>>
>>> Thanks,
>>> Ash
>>> http://www.ashleysheridan.co.uk
>>>
>>>
>>
>>
>> Hi Ash,
>> What about the "//" in  the beginning?
>>
>>
>> Karl DeSaulniers
>> Design Drumm
>> http://designdrumm.com
>>
>>
>
>
> As your example string didn't have a double slash I didn't write code
> for that, but it's easy enough to remove 0-length strings from the
> $directories array.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>

:) Rick's example, but how in your example do we look for a double
forward slash?
THX

Karl DeSaulniers
Design Drumm
http://designdrumm.com



You don't look for one, that's the point. The explode() breaks the string into an array at every occurrence of a '/' character. This will leave zero length strings in the array if there is a double // (which wasn't in any given example in this thread that I saw) When you use the array, just don't do anything with empty elements!

Thanks,
Ash
http://www.ashleysheridan.co.uk




Ahh.. that makes sense. Thanks Ash.


Karl DeSaulniers
Design Drumm
http://designdrumm.com


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux