Re: change URL variables




// this will replace (and/or add) parameters to the query string
$new_url = get_self_link(array('var'=>'new_value'));
// may seem like a lot of code, but I use these 2 funcs all the time.

function get_self_link($new_get_vars=array(),$full_url=false)
{
$string = '';
$get_vars = $_GET;
foreach ( $new_get_vars as $k=>$v )
$get_vars[$k] = $v;
$protocol = ( isset($_SERVER['HTTPS']) &&
$_SERVER['HTTPS']=='on' ) ? 'https' : 'http';
$current_url = $protocol.'://'.$_SERVER['HTTP_HOST'].
$_SERVER['REQUEST_URI'];
$url_parts = @parse_url($current_url);
$query = get_query_string($get_vars);
if ( $full_url )
$string = $protocol.'://'.$_SERVER['HTTP_HOST'];
$string .= $url_parts['path'].$query;
return $string;
}

/************************************************************/
// similar to http://www.php.net/function.http-build-query
// does not handle arrays
function get_query_string($attribs)
{
$query = '?';
foreach ( $attribs as $k=>$v )
{
if ( $v !== null && !is_array($v) )
{
$query .= $k;
$query .= '='.str_replace('%2F','/',urlencode($v));
$query .= '&';
}
}
$query = substr($query,0,-1);
$query = str_replace('&','&',$query);
return $query;
}

On Feb 28, 7:40 am, absc...@xxxxxxxxx wrote:
Hi out there,
I have an url that looks like this:http://host/file?var=value.
I am trying to replace the "value" part in my url and assign it to a
link on my page. I tried to parse the url but can only get the part
before the ? not included.

Is there any way to do so?

Thx.


.



Relevant Pages

  • Re: Dynamic access to ADO recordset?
    ... 'code to open a connection and create the query string ... 'close and destroy the connection if you are done with it ... Using arrays is more efficient, because there is no cursor library needed to ... This email account is my spam trap so I ...
    (microsoft.public.scripting.vbscript)
  • Re: How to extract variable and its value from URL
    ... You wouldn't even need a regular expression, you can just parse it apart ... on the equals sign and the ampersand (but a regular expression would work ... property to get the query string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Request.QueryString in javascript and null parameters
    ... I'm writing Javascript code to parse out a query string. ... But it fails if it contains a value. ...
    (comp.lang.javascript)
  • Re: Newbie: Regular expresion
    ... Jose Luis writes: ... is this a "how do I parse a query string" question in disguise? ...
    (comp.lang.perl.misc)
  • How to parse query string in CLI?
    ... This question is about whether something exists to parse a query string ... As this print from a CLI generated page indicates, ... Well, I can parse these strings myself, but I'd rather not if something will ... Obama: No hope, no change, more of the same. ...
    (comp.lang.php)