Ajax software
Free javascripts
↑
Main Page
If
$query string
is
a=1&aff_id=34&b=2
, then the
items
array will have these three elements:
$items[0] => ‘a=1’
$items[1] => ‘aff_id=34’
$items[2] => ‘b=2’
You then parse each of these items using the
foreach,
and you split each item on the
=
character using
explode
. Using the resulting data you build an associative array named
$qs_array
, which is returned at
the end. You use the
urldecode()
function when saving each query string parameter, because special
characters may have been encoded:
// initialize the return array
$qs_array = array();
// create the array
foreach($items as $i)
{
// split the name-value pair and save the elements to $qs_array
$pair = explode(‘=’, $i);
$qs_array[urldecode($pair[0])] = urldecode($pair[1]);
}
// return the array
return $qs_array;
}
We looked at
parse_query_string()
because it’s used by
remove_query_param()
, which is the
function of interest — as you remember, it’s called from
aff_test.php
to remove the
aff_id
parameter
from the query string.
The
remove_query_param()
function receives two parameters: the URL and the parameter you want to
remove from that URL. The URL will be something like
/aff_test.php?a=1&b=2
. Because the query
string is what you need for further processing,
remove_query_param()
starts by splitting the
$url
parameter into an
$url_path
and a
$query_string
:
// removes a parameter from the query string
function remove_query_param($url, $param)
{
// extract the query string from $url
$tokens = explode(‘?’, $url);
$url_path = $tokens[0];
$query_string = $tokens[1];
Next, you use the
parse_query_string()
function to transform the query string into an associative array:
// transform the query string into an associative array
$qs_array = parse_query_string($query_string);
As mentioned earlier, the associative array will be something like this:
‘a’ => ‘1’
‘aff_id’ => ‘34’
‘b’ => ‘2’
116
Chapter 5: Duplicate Content
c05.qxd:c05 10:41 116
Ajax software
Free javascripts
→