If the URL is https://www.mohsin.com?main=17&street=71, then what is the PHP code to grab the \"main\" and \"street\" values?
I am expecting line of codes written in
Just call the following function:
function getQueryParameter($url, $param) {
$parsedUrl = parse_url($url);
if (array_key_exists('query', $parsedUrl)) {
parse_str($parsedUrl['query'], $queryParameters);
if (array_key_exists($param, $queryParameters)) {
return $queryParameters[$param];
}
}
}
Example:
$parameter = getQueryParameter('https://www.mohsin.com?main=17&street=71', 'main')
will return 17
.$parameter = getQueryParameter('https://www.mohsin.com?main=17&street=71', 'street')
will return 71
.$parameter = getQueryParameter('https://www.mohsin.com?main=17&street=71', 'invalid')
will return null
.