PHP: Comparing parameters from 2 separate urls
During one of many late night coding sessions I was working on a project were I needed to find out if all the parameters from one url were present and had matching values to another url. My first instinct was to try a regular expression but I quickly realized that it wouldnt work since the parameters could be placed in any order within the query string. It did take me about 15 minutes to come to this conclusion so when I say ‘quickly’ I meant it was quick for 3:30 am. Here is what I eventually did came up with:
$qs1 = “a=123&b=hello&c=world”;
$qs2 = “c=world&a=123&d=again&b=hello”;
parse_str($qs1, $qsParams1);
parse_str($qs2, $qsParams2);
$matchingParams = array_intersect_assoc($qsParams1, $qsParams2);
if (count($matchingParams) == count($qsParams1)) {
$allParamsMatch = true;
} else {
$allParamsMatch = false;
}
So in this example the match ends up being true, even though there are more parameters in the second string and they are out of order it still contained all the values from the first string.
