From: Adam Richardson on
On Sun, Jun 13, 2010 at 9:29 PM, Robert Cummings <robert(a)interjinn.com>wrote:

> 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.
>>
>
> <?php
>
> function my_parse_url( $url )
> {
> $parsed = parse_url( $url );
> $parsed['file'] = basename( $parsed['path'] );
> $parsed['pathbits'] = explode( '/', ltrim( dirname( $parsed['path'] ),
> '/' ) );
>
> return $parsed;
> }
>
> $url = my_parse_url( 'http://foo.fee.com/blah/bleh/bluh/meh.php' );
> print_r( $url );
>
> ?>
>
> Cheers,
> Rob.
> --
> E-Mail Disclaimer: Information contained in this message and any
> attached documents is considered confidential and legally protected.
> This message is intended solely for the addressee(s). Disclosure,
> copying, and distribution are prohibited unless authorized.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Clean and lean, Robert ;) Nice!

Adam

--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com
From: tedd on
At 9:29 PM -0400 6/13/10, Robert Cummings wrote:
>
><?php
>
>function my_parse_url( $url )
>{
> $parsed = parse_url( $url );
> $parsed['file'] = basename( $parsed['path'] );
> $parsed['pathbits'] = explode( '/', ltrim( dirname(
>$parsed['path'] ), '/' ) );
>
> return $parsed;
>}
>
>$url = my_parse_url( 'http://foo.fee.com/blah/bleh/bluh/meh.php' );
>print_r( $url );
>
>?>
>
>Cheers,
>Rob.

Rob:

Very neat.

It also handles url's like this:

<http://mydomain.com/mydirectory/mysubdirectory/anothersubdirectory/mypage.php>

See Demo here:

http://www.webbytedd.com/b4/parse-url/index.php

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
From: Robert Cummings on
tedd wrote:
> At 9:29 PM -0400 6/13/10, Robert Cummings wrote:
>> <?php
>>
>> function my_parse_url( $url )
>> {
>> $parsed = parse_url( $url );
>> $parsed['file'] = basename( $parsed['path'] );
>> $parsed['pathbits'] = explode( '/', ltrim( dirname(
>> $parsed['path'] ), '/' ) );
>>
>> return $parsed;
>> }
>>
>> $url = my_parse_url( 'http://foo.fee.com/blah/bleh/bluh/meh.php' );
>> print_r( $url );
>>
>> ?>
>>
>> Cheers,
>> Rob.
>
> Rob:
>
> Very neat.
>
> It also handles url's like this:
>
> <http://mydomain.com/mydirectory/mysubdirectory/anothersubdirectory/mypage.php>
>
> See Demo here:
>
> http://www.webbytedd.com/b4/parse-url/index.php

It's useful to leverage the work of others. So using parse_url() gets
you all the parsing stuff for a url without having to worry about the
spec (such as embedded user, password, port, parameters, and fragment.
Then we just augment to provide the extra functionality :)

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.