PHP offers two functions that are very useful for us in generating or modifying a link:
- parse_str – automatically converts all query parameters into an array
- http_build_query – generates a URL-encoded query string from the associative (or indexed) array
Below is a function that can help you add, delete or modify a parameter in a url query
function manage_http_query($query, $key_value) { $aquery = []; parse_str($query, $aquery); if (!is_array($aquery)) return []; if (is_array($key_value)) { if (count($key_value) >= 1) { foreach ($key_value as $key=>$val) : if (isset($val)) { if (is_array($key_value[$key])) { foreach ($key_value[$key] as $akey=>$aval) : if (isset($aval)) $aquery[$key][$akey] = $aval; // ~~ add or change array-key elseif (isset($aquery[$key][$akey])) unset($aquery[$key][$akey]); // ~~ remove array-key by NULL endforeach; $aquery[$key] = array_values($aquery[$key]); // ~~ re-index array-key: [0,1,...n] } else { $aquery[$key] = $val; // ~~ add or change key by name } } elseif (isset($aquery[$key])) unset($aquery[$key]); // ~~ remove key by NULL endforeach; } } elseif (!empty($key_value) && isset($aquery[$key_value])) unset($aquery[$key_value]); // ~~ remove key by name return http_build_query($aquery); }
Usage example:
$q = 'foo=bar&bar=boom&key1=val1&key2[]=val21&key2[]=val22&key2[]=val23&test=1';
$modkey = ['bar'=>null, 'key2'=>['val2X',null,'val2Y'], 'test'=>0, 'key3'=>'val3'];
$q_result = manage_http_query($q, $modkey);
echo $q_result;
# foo=bar&key1=val1&key2%5B0%5D=val2X&key2%5B1%5D=val2Y&test=0&key3=val3
Explanations:
THE QUERY PARAMETER IS:
$q = 'foo=bar&bar=boom&key1=val1&key2[]=val21&key2[]=val22&key2[]=val23&test=1';
which in array format becomes:
Array ( [foo] => bar [bar] => boom [key1] => val1 [key2] => Array ( [0] => val21 [1] => val22 [2] => val23 ) [test] => 1 )
THE UPDATE / INSERT PARAMETER IS:
$modkey = ['bar'=>null, 'key2'=>['val2X',null,'val2Y'], 'test'=>0, 'key3'=>'val3'];
where:
'bar'=>null
= remove key ‘bar‘ from query
'key2'=>['val2X',null,'val2Y']
= update ‘key2[0]‘ with new value = ‘val2X‘, remove ‘key2[1]‘ and update ‘key2[2]‘ = ‘val2Y’
'test'=>0
= update or insert ‘test‘ key (in our case an update is made, thetest
parameter already exists)
'key3'=>'val3'
= update or insert ‘key3‘ (in our case the insert is made, thekey3
parameter does not exist)
* note: update for array parameters is not so simple, the number of parameters must be equal to the maximum expected, even if one is valid, the rest must be NULL!
THE RESULT OF THE FUNCTION IS:
foo=bar&key1=val1&key2%5B0%5D=val2X&key2%5B1%5D=val2Y&test=0&key3=val3
which in array format is:
Array ( [foo] => bar [key1] => val1 [key2] => Array ( [0] => val2X [1] => val2Y ) [test] => 0 [key3] => val3 )
So as you can see, using the function is relatively simple and covers almost any situation encountered for adding, modifying or deleting a parameter from a url.
- If you want to delete a single parameter, it is enough to call the function with parameter name, without being in array format.
$q_result = manage_http_query($q, 'key1');
key1
will be removed. - If you want to insert or change one or more parameters, they must be present in pair format: Key => Value
array('key1'=>'val1', 'key2'=>'val2', ...);
If the parameterkeyX
exists it will be modified, otherwise, if it does not exist it will be added. - If you want to add more parameter and delete others at the same time, the parameters programmed for deletion must be set to NULL:
array('key1'=>'val1', 'key2'=>NULL, ...);
Thekey2
parameter will be added or modified, and thekey2
will be deleted.
HappyCoding ♥ !
Leave a Reply
Your email address will not be published. Required fields are marked *