How can a function be created to check for specific key-value pairs in the query parameter after using parse_url in PHP?
After using parse_url in PHP to parse a URL and extract the query parameter, a function can be created to check for specific key-value pairs within the query parameter. This function can iterate through the key-value pairs and compare them with the specified criteria. If a match is found, the function can return true, indicating that the specific key-value pair exists in the query parameter.
function checkQueryParams($url, $key, $value) {
$parsed_url = parse_url($url);
parse_str($parsed_url['query'], $query_params);
foreach ($query_params as $param_key => $param_value) {
if ($param_key == $key && $param_value == $value) {
return true;
}
}
return false;
}
// Example usage
$url = "http://example.com/page?foo=bar&baz=qux";
$key = "foo";
$value = "bar";
if (checkQueryParams($url, $key, $value)) {
echo "Key-value pair found in query parameter!";
} else {
echo "Key-value pair not found in query parameter.";
}