Re: [PHP] String Parse Help for novice
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] String Parse Help for novice
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
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
Re: [PHP] String Parse Help for novice
On Jun 13, 2010, at 5:13 PM, 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Hi Rick, Just a thought, but cant you do something to separate them according to the / (forward slash)? maybe preg_replace or something. Sorry not much more help. Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
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
Re: [PHP] String Parse Help for novice
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
On Jun 13, 2010, at 5:35 PM, 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 try echo($url); and see Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
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
Re: [PHP] String Parse Help for novice
On Jun 13, 2010, at 5:35 PM, 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 Oops I meant echo($the_path); or echo both and see. Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
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
Re: [PHP] String Parse Help for novice
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
Re: [PHP] String Parse Help for novice
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
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
Re: [PHP] String Parse Help for novice
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
Re: [PHP] String Parse Help for novice
On Sun, Jun 13, 2010 at 9:29 PM, Robert Cummings rob...@interjinn.comwrote: 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