parse_url when url is a paramater?

pillmill used Ask the Experts™
Parse_url produces the wrong result the one of the parameters is itself a url:

For example,


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.  
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sr. System Analyst
you have to encode/decode url parameters before adding it to query string

for example say your parameter is this

encoded version will be

and pass this as


when you get myUrl, do the reverse, decode to get the original url ith parameters...
Also found this at  Don't know if it applies to you

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


Most Valuable Expert 2011
Top Expert 2016

Here is the demonstration script proving that HainKurt is on the ball.

Best to all, ~Ray
<?php // RAY_temp_pillmill.php

$url = '';
$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

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Sandeep KothariProject Lead

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

example code :

$theurl = "";

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


Open in new window

Most Valuable Expert 2011
Top Expert 2016

No points for this, please -- the question has already been answered.  The PHP URL functions are documented here:

These are the PHP functions that make it easy to get this right.

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 "", 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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial