Format a URL given GET data
Parameters
$url
stringrequired- Original URL.
$data
array|objectrequired- Data to build query using, see https://www.php.net/http_build_query
Source
if (!empty($data)) {
$query = '';
$url_parts = parse_url($url);
if (empty($url_parts['query'])) {
$url_parts['query'] = '';
} else {
$query = $url_parts['query'];
}
$query .= '&' . http_build_query($data, '', '&');
$query = trim($query, '&');
if (empty($url_parts['query'])) {
$url .= '?' . $query;
} else {
$url = str_replace($url_parts['query'], $query, $url);
}
}
return $url;
}
User Contributed Notes
You must log in before being able to contribute a note or feedback.