<?php

function url( $f_szUrlInfo = '', $f_bXHTMLalize = false )
{
	// If empty or no string inputted, return the current url
	if ( 0 == strlen(trim((string)$f_szUrlInfo)) )
	{
		return 0 < count($_GET) ? '?'.$_SERVER['QUERY_STRING'] : basename($_SERVER['SCRIPT_NAME']);
	}

	$szParamDelimiter = $f_bXHTMLalize ? '&amp;' : '&';

	// Create first (old) part
	$arrQueryParams = $_GET;

	// Create next (new) part
	$arrOldParts = explode("&", $f_szUrlInfo);

	foreach ( $arrOldParts AS $szParamPart )
	{
		list($key, $val) = explode("=", $szParamPart, 2);
		// this is new and not urlencode()d, so encode it now
		$arrQueryParams[urlencode($key)] = urlencode($val);
	}

	// The first char of the query_string is a '?'
	$szUrl = '?';

	foreach ( $arrQueryParams AS $szKey => $szVal )
	{
		$szUrl .= $szKey . '=' . $szVal . $szParamDelimiter;
	}

	return substr( $szUrl, 0, -1*strlen($szParamDelimiter) );
}

?>