Re: String Parse Help for novice

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

 



On Sun, 2010-06-13 at 18:52 -0400, Rick Dwyer wrote:

> OK, sorry for any confusion.
> 
> Here is all my code:
> 
> $url = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://". 
> $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
> $thepath = parse_url($url);
> 
> So, given that the URL can vary 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.
> 
>   --Rick
> 
> 
> 
> 
> 
> On Jun 13, 2010, at 6:42 PM, Ashley Sheridan wrote:
> 
> > On Sun, 2010-06-13 at 18:35 -0400, Rick Dwyer wrote:
> >
> >> OK, I get the following error:
> >>
> >> Warning: basename() expects parameter 1 to be string, array given  
> >> in....
> >>
> >> When I use the following:
> >>
> >> $thepath = parse_url($url);
> >> $filename = basename($thepath);
> >>
> >> Is my variable thepath not automatically string?
> >>
> >>  --Rick
> >>
> >>
> >> On Jun 13, 2010, at 6: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
> >>>
> >>>
> >>
> >>
> >
> >
> > Because you've given it an array. Your original question never  
> > mentioned
> > you were using parse_url() on the original array string. parse_url()
> > breaks the string into its component parts, much like my explode
> > example.
> >
> > Thanks,
> > Ash
> > http://www.ashleysheridan.co.uk
> >
> >
> 
> 


Take out the parse_url line and use the code I gave you, or keep the
parse_url line and drop my explode line.

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



[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