Link to home
Start Free TrialLog in
Avatar of pillmill
pillmill

asked on

parse_url when url is a paramater?

Parse_url produces the wrong result the one of the parameters is itself a url:

For example,

parse_str($theurl['query'],$params);  

will lose all of the params after the url parameter, because the "&"
delimiting the parameters is not recognized.

How can I fix this? I need all the parameters from a URL REST query, when
one of the parameter values is itself a URL.  
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also found this at php.net.  Don't know if it applies to you

URL's in the query string of a relative URL will cause a problem

fails:
/page.php?foo=bar&url=http://www.example.com

parses:
http://www.foo.com/page.php?foo=bar&url=http://www.example.com 
Here is the demonstration script proving that HainKurt is on the ball.
http://www.laprbass.com/RAY_temp_pillmill.php
Outputs:
http://www.hainkurt.com?a=1&b=2
http%3A%2F%2Fwww.hainkurt.com%3Fa%3D1%26b%3D2
http://www.hainkurt.com?a=1&b=2

Best to all, ~Ray
<?php // RAY_temp_pillmill.php
error_reporting(E_ALL);

$url = 'http://www.hainkurt.com?a=1&b=2';
$enc = urlencode($url);
$new = urldecode($enc);

echo "<pre>";
echo PHP_EOL . htmlentities($url);
echo PHP_EOL . htmlentities($enc);
echo PHP_EOL . htmlentities($new);

Open in new window

well you have to explode the parse string with & and before doing that you decode the parse string...

example code :

$theurl = "http://www.example.com?h=test&o=ok&urll=http://www.example.org&helo=bye&working=fine";

$param_arr = explode('&',$theurl);

print_r($param_arr);

Open in new window

No points for this, please -- the question has already been answered.  The PHP URL functions are documented here:
http://php.net/manual/en/ref.url.php

These are the PHP functions that make it easy to get this right.
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php

Best to all, ~Ray
I concur with Ray_Paseur that HainKurt's answer is the one to use.  If the variable "query" were a URL such as "http://www.example.com?x=y&z=3", it would have to be passed through urlencode() in order to keep the values for $_GET['x'] and $_GET['z']; without that, it is simply not possible to be 100% sure that all the attached variables (except maybe the first) remain attached to it.

@kshna: please be respectful of other experts. There is no reason that the user asking the question should have to deal with such childish outbursts. Your fix does not actually fix the problem, but merely illustrates the problem: Ray_Paseur was saying "no points" for the post it was stated in, indicating that HainKurt's answer was the one that should be awarded points.