Zend - The PHP Company




HTTP

Add Code


modify url  

Type: code fragment
Added by: tim
Entered: 02/03/2000
Last modified: 08/12/1999
Rating: - (fewer than 3 votes)
Views: 8385
Modify GET variables in a URL.


<?php
function modify_url($url$variable$value) {

  
$value urlencode($value);

  if (
ereg("(\?|&)$variable=([^&]*)(&|$)"$url)) {
    
/* Change the value in query string */
    
$new_url ereg_replace("(\?|&)$variable=([^\&]*)(&|$)","\1$variable=$value\3"$url);
  } else {
    
/* The variable doesn't exist in query string */
    
$parsed_url parse_url($url);
    
$new_url $parsed_url["query"] ? $url."&$variable=$value$url."?$variable=$value";
  }
  
$new_url ereg_replace("\?\?","?"$new_url);

  return 
$new_url;
}

?>


Usage Example


<?
 
echo modify_url($REQUEST_URI"bla""123");
?>


Rate This Script





Search



This Category All Categories